简体   繁体   English

avr-gcc:循环,> =快于>检查

[英]avr-gcc: loop with >= faster than > check

There is something I don't understand with my code. 我的代码中有一些我不理解的东西。 I'm working with avr-gcc and optimizing my code for speed. 我正在使用avr-gcc并优化我的代码以提高速度。 I've read about loops, counting down and post/pre (de)crements, comparison with 0 and discovered something odd. 我已经读过关于循环,倒计时和后/前(de)创造,与0比较并发现奇怪的东西。

This code runs in 47s: 此代码运行于47s:

UInt8 ret, i;
i = UJ_THREAD_QUANTUM;
do {
    ret = ujThreadPrvInstr(h, t);
    if (ret == UJ_ERR_RETRY_LATER) {
        // do not bother with the rest of time quantum if we're already stuck
        ret = UJ_ERR_NONE;      
        break;
    }
    died = (t->pc == UJ_PC_DONE);
    if (died) {
        break;
    }
    if (ret != UJ_ERR_NONE) {
        break;
    }
} while(--i);

This code in 43s: 此代码在43s:

for (i = UJ_THREAD_QUANTUM; i >= 0; --i) {
    ret = ujThreadPrvInstr(h, t);
    if (ret == UJ_ERR_RETRY_LATER) {
        // do not bother with the rest of time quantum if we're already stuck
        ret = UJ_ERR_NONE;
        break;
    }
    died = (t->pc == UJ_PC_DONE);
    if (died) {
        break;
    }
    if (ret != UJ_ERR_NONE) {
        break;
    }
}

This code in 47s again: 此代码再次出现在47s:

for (i = UJ_THREAD_QUANTUM+1; i > 0; --i) {
    ret = ujThreadPrvInstr(h, t);
    if (ret == UJ_ERR_RETRY_LATER) {
        // do not bother with the rest of time quantum if we're already stuck
        ret = UJ_ERR_NONE;      
        break;
    }
    died = (t->pc == UJ_PC_DONE);
    if (died) {
        break;
    }
    if (ret != UJ_ERR_NONE) {
        break;
    }
}

Thinking that I perhaps misunderstood some internal workings, I varied the value of UJ_THREAD_QUANTUM (which is 10 by default) and post/predecrement counters but eventually discovered that it appears that whether I use >= 0 or > 0 is the deciding factor. 考虑到我可能误解了一些内部工作,我改变了UJ_THREAD_QUANTUM (默认为10)和post / predecrement计数器的值,但最终发现看起来我使用>= 0> 0是决定因素。

Can anybody explain why this is? 任何人都可以解释为什么会这样吗?

UInt8 ret, i;

means i >= 0 is always true. 意味着i >= 0始终为真。 But i > 0 has to be evaluated. 但是必须评估i > 0

Forget everything that you read about loops, post increment, counting down to zero, and so on. 忘记你读到的关于循环,后递增,倒数到零等的所有内容。 This isn't even micro-optimisation, it is nano-optimisation. 这甚至不是微优化,它是纳米优化。 The only way it has any effect on the speed of your code is if you make a code that actually changes what the loop does. 它对代码速度有任何影响的唯一方法是,如果你创建的代码实际上改变了循环的作用。 That is if by playing around with your loop you introduced bugs. 那就是如果通过玩你的循环你引入了错误。

Especially in a case like this one, where you are playing around with threads - all quite expensive operations. 特别是在像这样的情况下,你正在玩线程 - 所有相当昂贵的操作。 What makes you think that it matters even one bit how you change your variables? 是什么让你认为你改变你的变量有点重要? Changing i is a matter of a nanosecond or less. 改变i是一个纳秒或更短的问题。 How on earth will this have any effect on code that runs for 43 seconds unless you do it tens of billions of times? 除非你做了数百亿次,否则这对于运行43秒的代码有什么影响呢?

For your loops, use code that is correct , and that is readable. 对于你的循环,使用正确且可读的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM