在C ++中比较的性能(foo> = 0对foo!= 0) [英] Performance of comparisons in C++ ( foo >= 0 vs. foo != 0 )

查看:144
本文介绍了在C ++中比较的性能(foo> = 0对foo!= 0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在一段代码中工作非常重要,我基本上有以下情况:

I've been working on a piece of code recently where performance is very important, and essentially I have the following situation:

int len = some_very_big_number;
int counter = some_rather_small_number;

for( int i = len; i >= 0; --i ){
    while( counter > 0 && costly other stuff here ){
        /* do stuff */
        --counter;
    }
    /* do more stuff */
}

所以这里我有一个非常频繁运行的循环,对于一定数量的运行,while块将被执行,直到变量 counter 减少为零,然后while循环将不会被调用,因为第一个表达式将是false。

So here I have a loop that runs very often and for a certain number of runs the while block will be executed as well until the variable counter is reduced to zero and then the while loop will not be called because the first expression will be false.

现在的问题是,如果使用

counter> 0 counter!= 0

我怀疑会有,有没有人知道这个。 p>

The question is now, if there is a difference in performance between using
counter > 0 and counter != 0?
I suspect there would be, does anyone know specifics about this.

推荐答案

你认为什么会解决你的问题! :D

Do you think that what will solve your problem! :D

    if(x >= 0)
00CA1011  cmp         dword ptr [esp],0 
00CA1015  jl          main+2Ch (0CA102Ch) <----
...
    if(x != 0)
00CA1026  cmp         dword ptr [esp],0 
00CA102A  je          main+3Bh (0CA103Bh) <----

这篇关于在C ++中比较的性能(foo> = 0对foo!= 0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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