Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 += Vs. |=
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DanielG




PostPosted: Tue Dec 23, 2008 6:08 pm   Post subject: += Vs. |=

if both can be used, which is generally faster?
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Tue Dec 23, 2008 6:55 pm   Post subject: RE:+= Vs. |=

Neither I assume, it all compiles to the same binary (I think).
btiffin




PostPosted: Tue Dec 23, 2008 6:56 pm   Post subject: RE:+= Vs. |=

Umm, addition and bitwise or are not generally interchangeable.

If you are or'ing together a bunch of flags, use |=. Never rely on O_WRONLY O_CREAT O_TRUNC (for instance) being values that you can just add together.

Performance should be secondary to correctness, always. And in this case, addition and bitwise or are so close, that it may not matter.

If you really need to know some numbers (what I grew up calling clock counts, no longer applicable) and assuming Intel (or similar)
http://www.intel.com/products/processor/manuals/index.htm

Cheers
Insectoid




PostPosted: Tue Dec 23, 2008 7:07 pm   Post subject: RE:+= Vs. |=

Well, that's what threw me off. I didn't know what |= did, just assumed his claim was correct.
DanielG




PostPosted: Wed Dec 24, 2008 1:48 am   Post subject: RE:+= Vs. |=

addition and binary are interchangeable if you are adding powers of two, and if you never add the same number twice, for that case, which would be faster?
btiffin




PostPosted: Wed Dec 24, 2008 11:26 am   Post subject: RE:+= Vs. |=

You'll have to look; The Intel manuals (mentioned above) should give a good clue on how your chip will perform those two operations.

Or run some tests on the target hardware. With current chip design and lookahead cache, parallelism, ..., it's not a general case call on whether OR or ADD will be a faster operation on any given set of circumstances. I'll guess over a billion runs both operations will complete within a few milliseconds difference, with the largest chunk of time being the data movement to from CPU to from bus to from storage. But again, very dependant on wiring, cache, what else is going on.

If you were designing a circuit board, OR may end being less gates, but again, modern hardware will have a complexity level that most humans couldn't easily grok. It used to be, with half-adder and full-adder gated AND/OR/NOT logic circuits, you couldn't add bit 1 to bit 1 until you knew the carry over from adding bit 0 to bit 0, so the operation was serialized; but even those days are gone with the newer highly complex adder circuits.

DanielG; I'm not trying to say "ignore it". I'm all for highly efficient programming and developers that care. You'll get the meatiest answer out of the chip manuals, and each one will give you a slightly different answer depending on design and conditions. Wink

As a for instance; old assembler pro's used to used
code:

XOR A0, A0

which was more efficient than
code:

MOV 0, A0

or even
code:

CLR A0

but those days are behind most of us.

Cheers
Edit; typos
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: