// Justin C. Miller
// University of Wisconsin Oshkosh
// Made for: http://www.geocities.com/neonprimetime.geo/index.html
// Date: 2001
// Borland Builder 4.0
// Left Shift vs. multiplying
#include
#include
#include
// thing to learn in this example
// is that a left shift is quicker
// than a multiplication when you
// are multiplying 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++); // just added to increase time, otherwise time would be 0 because it'd do it so quick
return x*2;
}
int test2(int x)
{
for(int i = 0 ; i < 10000000 ; i++); // just added to increase time
return (x << 1);
}
int main()
{
int num = 5 ;
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 << "5 * 2 = " << num1 << " Time:" << (end_time-start_time) << endl ;
cout << "5 << 1 = " << num2 << " Time:" << (et-st) << endl ;
return 0 ;
}
// Output
// 5 * 2 = 10 Time:170 units
// 5 >> 1 = 10 Time:110 units
               (
geocities.com/neonprimetime.geo/cpp)                   (
geocities.com/neonprimetime.geo)