重复一段代码固定的次数 [英] Repeat a block of code a fixed number of times

查看:65
本文介绍了重复一段代码固定的次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重复一段代码,不使用条件,但仍然只重复特定次数.

I'm trying to repeat a block of code, without using a condition, yet still only repeating it a specific number of times.

基本上是这样的:

repeat(50)
{
    //Do stuff here.
}

有没有办法做到这一点?除了复制粘贴50次?

Is there a way to do this? Other than copying and pasting 50 times?

我这样做是因为我想如果我知道我想重复多少次的话,这比每次都检查一个条件要快.那是准确的吗?还是我还要检查它重复了多少次?

I'm doing this because I figured if I know how many times I want to repeat something, it'd be quicker than checking a condition every time. Is that accurate? Or would I still be checking how many times it's been repeated?

基本上,它会更快吗?

推荐答案

您尝试通过使用某些构造(包括手动剪切和粘贴代码)来优化循环的执行速度来优化循环是不明智的.不要这样做;相反,它可能会取消优化"执行速度.

Your attempts to optimize the loop by using some construct (incl. manually cutting & pasting the code) to optimize the loop's execution speed are ill-advised. Don't do it; it would probably "un-optimize" the execution speed instead.

在我遇到的任何 C++ 实现中(MSVC 6.0、2003、2005、2010、GCC 各种版本、Diab 各种版本),绝对为零,抱歉我没有强调足够,零,涉及的时间分配循环计数变量,假设为分配循环计数变量的函数分配了任何其他变量.对于不进行函数调用的简单循环,循环计数变量甚至可能永远不会进入内存;它可以在其整个生命周期内完全保存在单个 CPU 寄存器中.即使它存储在内存中,它也会在运行时堆栈上,并且它(和任何其他局部变量)的空间将在单个操作中一次全部占用,这取决于数量的多少栈上分配的变量.像循环计数器变量这样的局部变量在堆栈上分配,堆栈分配是廉价的廉价廉价,与堆分配相反.

In any C++ implementation I've ever encountered (MSVC 6.0, 2003, 2005, 2010, GCC various versions, Diab various versions), there is absolutely zero, sorry I didn't stress that enough, ZERO, time involved with allocating a loop counting variable, assuming any other variables were allocated for the function in which the loop counting variable is allocated. For a simple loop that makes no function calls, the loop counting variable may never even make it out to memory; it may be held entirely in a single CPU register for its entire lifetime. Even if it is stored in memory, it would be on the runtime stack, and space for it (and any other local variables) would be claimed all at once in a single operation, which takes no more or less time depending on the number of variables allocated on the stack. Local variables like your loop counter variable are allocated on the stack, and stack allocations are CHEAP CHEAP CHEAP, as opposed to heap allocations.

堆栈上的示例循环计数器变量分配:

Example loop counter variable allocation on the stack:

for (int i=0; i<50; ++i) {
    ....
}

堆栈上的另一个循环计数器变量分配示例:

Another example loop counter variable allocation on the stack:

int i = 0;
for (; i<50; ++i) {
    ....
}

在堆上分配的示例循环计数器变量(不要这样做;这很愚蠢):

Example loop counter variable allocated on the heap (don't do this; it's stupid):

int* ip = new int;
for (*ip=0; *ip<50; ++(*ip)) {
    ....
}
delete ip;

现在解决尝试通过手动复制 & 来优化循环的问题.粘贴而不是使用循环 &计数器:

Now to address the issue of attempting to optimize your loop by manually copying & pasting instead of using a loop & counter:

您正在考虑做的是一种手动形式的循环展开.循环展开是一种优化,编译器有时会使用它来减少循环中涉及的开销.只有在编译时可以知道循环的迭代次数(即迭代次数是常数,即使该常数涉及基于其他常数的计算),编译器才能执行此操作.在某些情况下,编译器可能会确定展开循环是值得的,但通常不会完全展开循环.例如,在您的示例中,编译器可能会确定将循环从 50 次迭代展开到仅具有 5 个循环体副本的 10 次迭代将具有速度优势.循环变量仍然存在,但不是对循环计数器进行 50 次比较,现在代码只需进行 10 次比较.这是一种权衡,因为循环体的 5 个副本占用了缓存中 5 倍的空间,这意味着加载相同指令的那些额外副本会强制缓存驱逐(抛出)已经存在的许多指令缓存以及您可能希望保留在缓存中的内容.此外,在循环根本没有展开的情况下,从主内存加载循环体指令的这 4 个额外副本比简单地从缓存中抓取已经加载的指令花费的时间要长得多.

What you're considering doing is a manual form of loop unrolling. Loop unrolling is an optimization that compilers sometimes use for reducing the overhead involved in a loop. Compilers can do it only if the number of iterations of the loop can be known at compile time (i.e. the number of iterations is a constant, even if the constant involves computation based on other constants). In some cases, the compiler may determine that it is worthwhile to unroll the loop, but often it won't unroll it completely. For instance, in your example, the compiler may determine that it would be a speed advantage to unroll the loop from 50 iterations out to only 10 iterations with 5 copies of the loop body. The loop variable would still be there, but instead of doing 50 comparisons of the loop counter, now the code only has to do the comparison 10 times. It's a tradeoff, because the 5 copies of the loop body eat up 5 times as much space in the cache, which means that loading those extra copies of the same instructions forces the cache to evict (throw out) that many instructions that are already in the cache and which you might have wanted to stay in the cache. Also, loading those 4 extra copies of the loop body instructions from main memory takes much, much longer than simply grabbing the already-loaded instructions from the cache in the case where the loop isn't unrolled at all.

总而言之,只使用循环体的一个副本并继续保留循环逻辑通常更有利.(即根本不进行任何循环展开.)

So all in all, it's often more advantageous to just use only one copy of the loop body and go ahead and leave the loop logic in place. (I.e. don't do any loop unrolling at all.)

这篇关于重复一段代码固定的次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆