C/C++: Multiply, or bitshift then divide? -


this question has answer here:

where it's possible so, i'm wondering if it's faster replace single multiplication bitshift followed integer division. i've got int k , want multiply 2.25.

what's faster?

int k = 5; k *= 2.25; std::cout << k << std::endl; 

or

int k = 5; k = (k<<1) + (k/4); std::cout << k << std::endl; 

output

11 11 

both give same result, can check this full example.

the first attempt

i defined functions regularmultiply() , bitwisemultiply() follows:

int regularmultiply(int j) {     return j * 2.25; }  int bitwisemultiply(int k) {     return (k << 1) + (k >> 2); } 

upon doing profiling instruments (in xcode on 2009 macbook os x 10.9.2), seemed bitwisemultiply executed 2x faster regularmultiply.

enter image description here

the assembly code output seemed confirm this, bitwisemultiply spending of time on register shuffling , function returns, while regularmultiply spent of time on multiplying.

regularmultiply:

enter image description here

bitwisemultiply:

enter image description here

but length of trials short.

the second attempt

next, tried executing both functions 10 million multiplications, , time putting loops in functions function entry , leaving wouldn't obscure numbers. , time, results each method took 52 milliseconds of time. @ least relatively large not gigantic number of calculations, 2 functions take same time. surprised me, decided calculate longer , larger numbers.

the third attempt

this time, multiplied 100 million through 500 million 2.25, bitwisemultiply came out slower regularmultiply.

the final attempt

finally, switched order of 2 functions, see if growing cpu graph in instruments perhaps slowing second function down. still, regularmultiply performed better:

enter image description here

here final program looked like:

#include <stdio.h>  int main(void) {     void regularmultiplyloop(int j);     void bitwisemultiplyloop(int k);      int i, j, k;      j = k = 4;     bitwisemultiplyloop(k);     regularmultiplyloop(j);      return 0; }  void regularmultiplyloop(int j) {     for(int m = 0; m < 10; m++)     {         for(int = 100000000; < 500000000; i++)         {             j = i;             j *= 2.25;         }         printf("j: %d\n", j);     } }  void bitwisemultiplyloop(int k) {     for(int m = 0; m < 10; m++)     {         for(int = 100000000; < 500000000; i++)         {             k = i;             k = (k << 1) + (k >> 2);         }         printf("k: %d\n", k);     } } 

conclusion

so can this? 1 thing can optimizing compilers better people. , furthermore, optimizations show more when there lot of computations, time you'd want optimize anyway. unless you're coding optimizations in assembly, changing multiplication bit shifting won't much.

it's think efficiency in applications, gains of micro-efficiency not enough warrant making code less readable.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -