Quake3World.com Forums
     Programming Discussion
        Operator performance


Post new topicReply to topic
Login | Profile | | FAQ | Search | IRC




Print view Previous topic | Next topic 
Topic Starter Topic: Operator performance

god xor reason
god xor reason
Joined: 08 Dec 1999
Posts: 21100
PostPosted: 01-22-2010 02:29 PM           Profile   Send private message  E-mail  Edit post Reply with quote


I haven't had any luck searching for the answer and I'm too lazy to code up my own test (maybe I will if no one knows). It is probably language dependent....but is there any performance difference between the two following code samples:

Code:
uint length = someArray.length - 1;
uint n = 0;
while (n != length) {n++;}


Code:
uint length = someArray.length;
uint n = 0;
while (n < length) {n++;}


To a human brain, less/greater than would seem to require an extra step compared to not equal. But when it comes to data in a computer it seems like you would have to compare every bit in until one didn't match in both scenarios. My guess is that not equal is still faster because you can immediately calculate half of all possible comparisons based on the first bit alone.




Top
                 

Commander
Commander
Joined: 15 Nov 2006
Posts: 129
PostPosted: 01-23-2010 02:15 AM           Profile Send private message  E-mail  Edit post Reply with quote


It's not just language dependent, it is compiler and CPU dependent (and if you're in a language with operator overloading, dependent on whether you or the framework has implemented overriden < and != operators for that type).

On an x86 cpu, probably both will probably be translated into a "cmp" instruction followed by either "jne" (jump not equal), or "jb" (jump if below). Both of them should perform similarly, since the cmp instruction is the same, and both jne/jb are simply testing flags that are set by cmp.

However to be sure you'd need to check the cpu instruction timing charts for your CPU, and confirm that the compiler is generating the "obvious" assembler for your code.




Top
                 
Quake3World.com | Forum Index | Programming Discussion


Post new topic Reply to topic


cron
Quake3World.com
© ZeniMax. Zenimax, QUAKE III ARENA, Id Software and associated trademarks are trademarks of the ZeniMax group of companies. All rights reserved.
This is an unofficial fan website without any affiliation with or endorsement by ZeniMax.
All views and opinions expressed are those of the author.