Test shift and multiplication comparison, found that the shift is faster than the multiplication operation of a bit speed, but the difficulty is to determine whether it is 2 of the power of the second number, if not also through the code to split to 2 Power + on the molecular fatigue and then through the shift to get 2 of the power of the number of such;
The following code is simply the judgment that the power series is the shift operation, not the normal calculation, not exactly the shift calculation, (after finding a method to judge faster in the update)
C + +:
1#include <iostream>2 3 4 5uint64_t multiply_dived (uint64_t member, uint64_t denominator,Charstyle)6 {7 //determines whether the denominator is a power of 28 if(Denominator & (Denominator-1))9 {Ten if(style = ='*') One { A while((Denominator >>=1) !=1) - { -Member <<=1; the } - -}Else - { + while((Denominator >>=1) !=1) - { +Member >>=1; A } at - } - returnmember; - } - - if(style = ='*') in { - returnMember *denominator; to}Else + { - returnMember/denominator; the } * } $ Panax Notoginsenguint64_t Nomorl (uint64_t member, uint64_t denominator,Charstyle) - { the if(style = ='*') + { A returnMember *denominator; the}Else + { - returnMember/denominator; $ } $ } - - the intMain () - {Wuyistd::clock_t start =0, stop =0; theStart =clock (); - for(Uint64_t i =0; I <100000000; i++) Wu { -Multiply_dived (1, -,'/'); About } $Stop =clock (); -Std::cout <<"In addition to shift run time:"<< (Double) (Stop-start)/clocks_per_sec) <<Std::endl; - - A + theStart =clock (); - for(Uint64_t i =0; I <100000000; i++) $ { theNomorl (1, -,'/'); the } theStop =clock (); theStd::cout <<"Normal except run time:"<< (Double) (Stop-start)/clocks_per_sec) <<Std::endl; - in return 0; the}
C + + shift calculation instead of multiplication operation