【发布时间】:2012-12-01 18:18:34
【问题描述】:
像这样拆分for-loop 的开销是多少,
int i;
for (i = 0; i < exchanges; i++)
{
// some code
// some more code
// even more code
}
像这样进入多个for-loops?
int i;
for (i = 0; i < exchanges; i++)
{
// some code
}
for (i = 0; i < exchanges; i++)
{
// some more code
}
for (i = 0; i < exchanges; i++)
{
// even more code
}
代码对性能敏感,但执行后者会显着提高可读性。 (以防万一,每个循环中除了少数访问器外,没有其他循环、变量声明或函数调用。)
我并不是一个低级编程大师,所以如果有人可以衡量与基本操作相比的性能损失,那就更好了,例如“每个额外的for -loop 将花费相当于两次 int 分配的费用。”但是,如果不是那么简单,我理解(并且不会感到惊讶)。
非常感谢,提前。
【问题讨论】:
-
这取决于平台。如果涉及缓存,则降级可能很明显
-
对此的唯一答案是:分析它。
-
我建议测试和测量。
标签: c++ performance for-loop