+ 1

What is faster, (a /= 2) or (a >>= 1)?

Variable a is an int. I suppose a >>= 1, but I want an answer which is not based on speculation.

13th Jul 2016, 2:10 PM
this->getName()
9 Antworten
+ 2
this->getName() an answer without speculation cannot be given without more details as the performance of the code depends on the compiler (including patches, versions, switches etc.). As Vedant said, you have to try it to know exactly.
13th Jul 2016, 3:39 PM
Stefan
Stefan - avatar
+ 1
this->getName() I have a small advise for you....as we jump between different IDE the basic functionality remains same in the compiler and plus Mobiles are usually considered for their portability but let me say you .... please do not use Mobiles to make programs or test complex things, I know mobiles are more preferable but with portability you loose usability
13th Jul 2016, 3:49 PM
Vedant Patadia
Vedant Patadia - avatar
0
Very likely bit shift, if there's any difference. But, you know, this simple thing only works on integer types and unless you really need to execute this statement *a lot* it's better to use the "/" as it expresses the intent in a way that everyone with miniscule C++ experience can understand.
13th Jul 2016, 2:57 PM
Stefan
Stefan - avatar
0
@Kushagra >> is bitwise right shift operator.
13th Jul 2016, 2:59 PM
this->getName()
0
But when the operations are always with a power of two, maybe the difference in time is significant. Or maybe the compiler knows that the divisor is a power of two and translates it into >>, so that the needed time is the same.
13th Jul 2016, 3:02 PM
this->getName()
0
Your example is a corner case, as I insinuated in my original answer. Though it's irrelevant for "normal" application programming as division by two is not done very often in this domain but it's a neat trick that shows deeper understanding of number formats (great!) and might be very efficient (also great!) in corner cases.
13th Jul 2016, 3:14 PM
Stefan
Stefan - avatar
0
try it yourself search for "time.h" header file on Google and set timers for both functions...take readings and then make conclusions...and yes be sure to post here the results
13th Jul 2016, 3:15 PM
Vedant Patadia
Vedant Patadia - avatar
0
Time.h does not have such high precision timer. chrono yes, but I think it cannot be used with this app. Also, the time appeared in this app maybe changes from time to time, because internet connection is needed, i dont know.
13th Jul 2016, 3:34 PM
this->getName()
- 1
these ">>" symbol are not use as, they are used in cout and cin .
13th Jul 2016, 2:57 PM
Kushagra Upadhyaya
Kushagra Upadhyaya - avatar