开发者

> and >= at Assembly level

开发者 https://www.devze.com 2023-04-01 04:42 出处:网络
I two time_t variables say start_sec and end_sec. Now I\'ve to perform million operations of finding out whether a given random time_t falls in this r开发者_StackOverflow中文版ange.

I two time_t variables say start_sec and end_sec. Now I've to perform million operations of finding out whether a given random time_t falls in this r开发者_StackOverflow中文版ange.

if(given_sec >= start_sec && given_sec <= end_sec) {
    /* given_sec falls in the interval*/
}

or

start_sec -= -1; //keep it for million comparisons
end_sec += 1; //keep it for million comparisons

if(given_sec > start_sec && given_sec < end_sec) {
    /* given_sec falls in the interval*/
}

which one is better?

I guess at the assembly level je loc; jle loc; jg loc; jge locare used to jump. I was wondering if jg loc; jl locsave any CPU cycles.


No, they don't save anything and if they were, optimizer would help you anyway.


A perfect example of premature optimization at the micro level, worrying about single CPU cycles, that gives you nothing, but obfuscates your code -- start_sec and end_sec no longer contain the values that the names suggest. And you even optimized it wrong:

start_sec -= -1; //keep it for million comparisons

This will add one to start_sec.

Where do the millions of time_t values come from? An input file? A database? From the system clock? A random generator? Each of these sources will be magnitudes slower than the comparison. So even if < and > are some CPU cycles faster than <= and >= (for your given instruction set on your given specific CPU), and profiling shows that this loop is in fact your bottleneck, this "optimization" is unlikely to solve this problem. Especially for just a few million comparisons on modern GHz CPUs with billions of cycles per second.

0

精彩评论

暂无评论...
验证码 换一张
取 消