Skip to main content
GameDev.net gamedev.net
🔒 Locked

Assignment, or Comparison and Assignment?

Started by jengerer Nov 20, 2009 at 11:07 PM 6 replies 1.1k views
Original Post
jengerer
jengerer
Hey, I'm curious about the efficiency difference between a comparison and an assignment. If I have an array of several objects, and I want to give all that have an attribute X the attribute Y (which is the attribute that non-X elements have, i.e, the elements that are not X are Y). Would it be more efficient to go through the array and assign Y to those that are X, or simply assign Y to all elements (even though there may be some that are already Y)? I bet the difference is minimal, but I'm just curious. To make this more clear, I'll consider a binary case. Imagine there's an array of [1, 0, 1, 1, 1, 0], and I want to set the 1s to 0s. Should I just go through the array, and set all of them to 0, or look for ones and set them to 0? Thanks, Jengerer
alvaro
alvaro
It will be faster to blindly assign Y to all elements. But you can easily test both options and measure.
Rattenhirn
Rattenhirn
That depends on how expensive the assignments and comparisons are. I know that this is exactly the question you asked, but we can't answer them for you, because you didn't specify what types X and Y are.

If those are primitive types (i.e. int), then blindly assigning will very likely be faster, as alvaro already said.
iMalc
iMalc
No memset implementation I know of performs a read of the buffer being written to, so there's a good chance that it's always faster to just perform the write.
Rattenhirn
Rattenhirn
Quote:
Original post by iMalc
No memset implementation I know of performs a read of the buffer being written to, so there's a good chance that it's always faster to just perform the write.


That's right, but the comparison might not have to read the complete buffer.
For instance, if the type in question was a string, the test for equality usually only needs to check the first few characters, and not the whole string.

Also, consider the case where the type is a pointer or reference to something. Then the assignment may involve freeing and allocating heap memory, which usually isn't that quick.

All in all, there are a lot of things to consider and there's no clear cut answer. If speed in this case is really a big concern, it's best to simply measure it (as always).
kyoryu
kyoryu
What is it you actually want to do? Set all the 1s to 0s, or set everything to 0?

While efficiency is important, correctness and maintainability are generally more important (and you can figure out where efficiency is important and optimize later, if necessary).

If these are truly bits, then the operations may be equivalent. If they're not (you're talking about attributes and objects), then while they may be equivalent AT THE MOMENT, they may not be equivalent in the future.

If you have an enum with values of CONNECTED and NOT_CONNECTED, then setting everything that was CONNECTED to NOT_CONNECTED may be equivalent to setting everything to NOT_CONNECTED, at least initially. But, if you later add a CONNECTING value, the two operations become entirely different, and will likely cause unexpected issues.

Semantically, one is SetAllConnectedToDisconnected, while one is SetEverythingToDisconnected. These do not represent the same intent.

Also, the relative efficiency of comparison vs. assignment is going to depend on what exactly is being compared/assigned. And the importance of it at all is going to depend on where in your code, and how often it's called. For the vast, vast majority of any code base, it simply won't matter at all.
jengerer
jengerer
What I had was an array of objects that had an enumerator attribute m_eSelection, and I wanted to set all of the ones with SELECT_DRAG to SELECT_NONE. However, it's not really an issue anymore because I realized there was a way to have only one be SELECT_DRAG at a time, so I only have to change one.

Thanks for the responses, though. Very helpful.
iMalc
iMalc
Just a tip, next time it would pay to mention which programming language you are using. We're clearly not all assuming the same language, but our differing answers are each equally correct given the language we each had in mind.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.