Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

double-break performance

By: a guest on Nov 30th, 2012  |  syntax: C++  |  size: 1.93 KB  |  views: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2. static int find = 2500;
  3. static int sideEffect = 0;
  4.  
  5. bool function1()
  6. {
  7.   sideEffect *= find;
  8.   bool found = false;
  9.   for (int bar =0; !found && bar < 100; ++bar)
  10.     for(int foo = 0; !found && foo < 100; ++foo)
  11.       if (foo * bar == find)
  12.         found = true;
  13.   return found;
  14. }
  15. bool function2()
  16. {
  17.   sideEffect *= find;
  18.   for (int bar =0; bar < 100; ++bar)
  19.     for(int foo = 0; foo < 100; ++foo)
  20.       if (foo * bar == find)
  21.         return 1;
  22.   return 0;
  23. }
  24. bool function3()
  25. {
  26.   sideEffect *= find;
  27.   bool result = false;
  28.   for (int bar =0; bar < 100; ++bar)
  29.     for(int foo = 0; foo < 100; ++foo)
  30.       if ( (result=(foo * bar == find)) )
  31.         goto end;
  32. end:
  33.   return result;
  34. }
  35. bool function4()
  36. {
  37.   try
  38.   {
  39.   sideEffect *= find;
  40.   for (int bar =0; bar < 100; ++bar)
  41.     for(int foo = 0; foo < 100; ++foo)
  42.       if (foo * bar == find)
  43.         throw true;
  44.   }
  45.   catch(bool b) { return b; }
  46.   return 0;
  47. }
  48.  
  49. void Test()
  50. {
  51.         scanf("%d", &find);
  52.         u8 stackMem[1024];
  53.         StackAlloc stack( stackMem, 1024 );
  54.         Scope scope( stack, "main" );
  55.         Timer& timer = *eiNew(scope, Timer)();
  56.  
  57.         int r = 0;
  58.         double b, e;
  59.         b = timer.Elapsed();
  60.         for( int i=0; i!=10000; ++i )
  61.                 r += (int)function2();
  62.         e = timer.Elapsed();
  63.         double t1 = e-b;
  64.  
  65.         b = timer.Elapsed();
  66.         //for( int j=0; j!=10000; ++j )
  67.         for( int i=0; i!=100000; ++i )
  68.                 r += (int)function2();
  69.         e = timer.Elapsed();
  70.         t1 = e-b;
  71.         b = timer.Elapsed();
  72.         //for( int j=0; j!=10000; ++j )
  73.         for( int i=0; i!=100000; ++i )
  74.                 r += (int)function1();
  75.         e = timer.Elapsed();
  76.         double t2 = e-b;
  77.         b = timer.Elapsed();
  78.         //for( int j=0; j!=10000; ++j )
  79.         for( int i=0; i!=100000; ++i )
  80.                 r += (int)function3();
  81.         e = timer.Elapsed();
  82.         double t3 = e-b;
  83.         b = timer.Elapsed();
  84.         //for( int j=0; j!=10000; ++j )
  85.         for( int i=0; i!=100000; ++i )
  86.                 r += (int)function4();
  87.         e = timer.Elapsed();
  88.         double t4 = e-b;
  89.  
  90.         printf( "%f, %f, %f, %f\n %d\n", t1, t2, t3, t4, r+sideEffect );
  91. }
clone this paste RAW Paste Data