// Justin C. Miller
// University of Wisconsin Oshkosh
// Made for: http://www.geocities.com/neonprimetime.geo/index.html
// Date: 2001
// Borland Builder 4.0
// Right Shift vs. dividing
#include
#include
#include
// thing to learn in this example
// is that a right shift is quicker
// than a division when you
// are dividing by a mulitple of 2
//
// (x >> 1) == (x / 2)
// (x >> 3) == (x / 8)
// (x ...............)
// (x >> n) == (x / (2^n))
int test1(int x)
{
for(int i = 0 ; i < 10000000 ; i++);
return x/2;
}
int test2(int x)
{
for(int i = 0 ; i < 10000000 ; i++);
return (x >> 1);
}
int main()
{
int num = 10 ;
long int start_time = clock() ;
int num1 = test1(num) ;
long int end_time = clock() ;
long int st = clock() ;
int num2 = test2(num) ;
long int et = clock() ;
cout << "10 / 2 = " << num1 << " Time:" << (end_time-start_time) << endl ;
cout << "10 >> 21 = " << num2 << " Time:" << (et-st) << endl ;
return 0 ;
}
// Output
// 10 * 2 = 5 Time:170 units
// 10 >> 1 = 5 Time:110 units
               (
geocities.com/neonprimetime.geo/cpp)                   (
geocities.com/neonprimetime.geo)