PDA

View Full Version : Which is most processor efficient?


maskedMan
06-03-2008, 07:05 PM
So, there are a lot of ways to test equivalency. I'm trying to figure out which of these methods is the least processor intensive:

if(!x){}

if(x==0){}

if(x===0){}

I'm going to go out on a limb and guess that the last one is the least intensive, as it only checks *specifically* for a Number value of 0 rather than testing whether it is any of the following : 0, undefined, null, false

Am I correct in this assumption?

ASWC
06-03-2008, 11:52 PM
The three of them will have the same impact on the processor which is .... not very much!

yell0wdart
06-04-2008, 07:30 AM
I agree. You've probably got bigger things to worry about than how many cycles it takes any given CPU to compute a basic comparison. If speed is that big a concern, you're better off writing your app in C. :p

jsebrech
06-04-2008, 08:06 AM
I doubt that the last one is least intensive, because it has to do a type check and then an equivalency test. If the type is always a match, the type check is superfluous code.

If you really want to know, just test it. Build a set of loops to run each of these thousands of times, and measure the elapsed time.

BernzSed
06-10-2008, 07:30 AM
I'm going to go ahead and guess (though I could be wrong) that the first two end up getting compiled to the same bytecode.


You know, at a certain level, optimization of code become less important than readability and maintainability. Unless there's some big important reason to make this single statement take a nanosecond less to execute, just write whatever code makes the most sense.
There are some low-level coders out there infamous for writing code that's slightly more optimized than normal, but impossible to re-use because nobody can read it without getting a headache.

Flash Gordon
06-10-2008, 08:40 AM
eh, who cares???

couple of big points that DO make a difference:

multiples of 256 for images can be automit mapped
image sized of 256 or less have faster support from the process.
and multiplication by .5 is faster than dividing by 2.