Which way is faster? operations with int

I need to do the following process:

At first there are two unsigned long long int variables A and B equal to each other. A is used in a function that changes the bit in the position N (from right to left starting at 0) from 0 to 1 by doing A += C, where C is 2^N and is already calculated. Then I have to make A equal to B again.

Two trivial codes come to mind:

1
2
function(N); // A is changed
A = B;


and

1
2
function(N); // A is changed
A -= C;


I want to know which of these is faster and also if there is another, more advanced, way even faster.
What is purpose of skipping few processor commands? "faster" in this case is not really faster - you will(most likely) not notice difference, and if it's processed several times(millions, maybe more) - then eventual difference will be still less than I second - at least that's most likely.

Probably A = B will be faster, because you will not have to do subtraction operation, but that's not an optimization you should look for.

PS. Also, why change A when you have to reset it after function is done? If you want to change N, change N, not A. If you are using pointer, maybe pass A by value instead of passing A by pointer or reference?

Also,
http://stackoverflow.com/questions/1897821/performance-of-c-operators
http://www.tantalon.com/pete/cppopt/asyougo.htm
On the second website there are some optimizations showed that you can do, but most of the time, many simple operations on POD types are taking similar amount of time.
Last edited on
Do it the right way -- the compiler will optimize properly for you.

A = B;
Topic archived. No new replies allowed.