Advertisement
jacknpoe

Recursive and For factorial benchmarks (plus inline)

Oct 31st, 2013
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.40 KB | None | 0 0
  1. /* Typical Output:
  2.  
  3. Resolution: 1000 ticks per second.
  4.  
  5. Number of tests (1-10): 4
  6. Number of dozens of millions iterations per test (1-20): 5
  7. Fatorial cut times 100 for test 1(1-10): 1
  8.  
  9. Please, configure the enviroment before continue.
  10. Press any key to continue. . .
  11.  
  12.  
  13. Foo factorial: 3.016s (549428545)
  14.  
  15. Test 1
  16. Recursive factorial: 2.141s (549428545)
  17. Inline recursive factorial: 1.659s (549428545)
  18. For Factorial: 1.435s (549428545)
  19. Inline for factorial: 1.442s (549428545)
  20.  
  21. Test 2
  22. Recursive factorial: 3.153s (274714273)
  23. Inline recursive factorial: 3.164s (274714273)
  24. For Factorial: 2.904s (274714273)
  25. Inline for factorial: 2.893s (274714273)
  26.  
  27. Test 3
  28. Recursive factorial: 4.639s (141155614)
  29. Inline recursive factorial: 4.652s (141155614)
  30. For Factorial: 4.375s (141155614)
  31. Inline for factorial: 4.389s (141155614)
  32.  
  33. Test 4
  34. Recursive factorial: 6.147s (137357137)
  35. Inline recursive factorial: 6.146s (137357137)
  36. For Factorial: 5.901s (137357137)
  37. Inline for factorial: 5.865s (137357137)
  38.  
  39. Press any key to continue. . .
  40.  
  41. */
  42.  
  43. #include <iostream>
  44. #include <cstdlib>
  45. #include <time.h>
  46.  
  47. //#define ITERATIONS 50000000
  48. //#define QUANTTESTS 4
  49. //#define FACTORIALCUT 100
  50.  
  51. inline long inline_factorial( long n) { return n ? n * inline_factorial(n-1) : 1; }
  52.  
  53. long factorial( long n) {
  54.   if(n == 0)
  55.     return 1;
  56.   else
  57.     return n * factorial(n-1);
  58. }
  59.  
  60. inline long inline_for_factorial( long n) {
  61.     long temp = 1;
  62.     for( long index = 1; index <= n; index++) temp *= index;
  63.     return temp;
  64. }
  65.  
  66. long for_factorial( long n) {
  67.     long temp = 1;
  68.     for( long index = 1; index <= n; index++) temp *= index;
  69.     return temp;
  70. }
  71.  
  72. inline long foo_factorial( long n) {
  73.     long temp = 1;
  74.     for( long index = 1; index <= n; index++) temp *= index;
  75.     return temp;
  76. }
  77.  
  78. int main() {
  79.     clock_t clock1, clock2;
  80.     long accumulator;
  81.     long ITERATIONS, QUANTTESTS,  FACTORIALCUT;
  82.  
  83.     std::cout.precision(5);
  84.  
  85.     std::cout << "Resolution: " << CLOCKS_PER_SEC << " ticks per second." << std::endl << std::endl;
  86.  
  87.     std::cout << "Number of tests (1-10): ";
  88.     std::cin >> QUANTTESTS;
  89.     if( QUANTTESTS < 1 or QUANTTESTS > 10 ) return 1;
  90.  
  91.     std::cout << "Number of dozens of millions iterations per test (1-20): ";
  92.     std::cin >> ITERATIONS;
  93.     if( ITERATIONS < 1 or ITERATIONS > 20 ) return 1;
  94.     ITERATIONS *= 10000000;
  95.  
  96.     std::cout << "Fatorial cut times 100 for test 1(1-10): ";
  97.     std::cin >> FACTORIALCUT;
  98.     if( FACTORIALCUT < 1 or FACTORIALCUT > 10 ) return 1;
  99.     FACTORIALCUT *= 100;
  100.  
  101.     std::cout << std::endl << "Please, configure the enviroment before continue."  << std::endl;
  102.     system( "PAUSE");
  103.  
  104.     // To avoid cache differences
  105.     accumulator = 0;
  106.     clock1 = clock();
  107.     for( int index = 0; index <= ITERATIONS; index++)
  108.         accumulator += foo_factorial( index % FACTORIALCUT);
  109.     clock2 = clock();
  110.  
  111.     std::cerr << std::endl << std::endl << "Foo factorial: " << double( (clock2 - clock1 )) / CLOCKS_PER_SEC << "s"
  112.               << " (" << accumulator << ")" << std::endl;
  113.  
  114.     for( int i = 1; i <= QUANTTESTS; i++) {
  115.  
  116.         std::cout << std::endl << "Test " << i << std::endl;
  117.  
  118.         accumulator = 0;
  119.         clock1 = clock();
  120.         for( int index = 0; index <= ITERATIONS; index++)
  121.             accumulator += factorial( index % (FACTORIALCUT * i));
  122.         clock2 = clock();
  123.    
  124.         std::cout << "Recursive factorial: " << double( (clock2 - clock1 )) / CLOCKS_PER_SEC << "s"
  125.                   << " (" << accumulator << ")" << std::endl;
  126.    
  127.         accumulator = 0;
  128.         clock1 = clock();
  129.         for( int index = 0; index <= ITERATIONS; index++)
  130.             accumulator += inline_factorial( index % (FACTORIALCUT * i));
  131.         clock2 = clock();
  132.    
  133.         std::cout << "Inline recursive factorial: " << double( (clock2 - clock1 )) / CLOCKS_PER_SEC << "s"
  134.                   << " (" << accumulator << ")" << std::endl;
  135.    
  136.         accumulator = 0;
  137.         clock1 = clock();
  138.         for( int index = 0; index <= ITERATIONS; index++)
  139.             accumulator += for_factorial( index % (FACTORIALCUT * i));
  140.         clock2 = clock();
  141.    
  142.         std::cout << "For Factorial: " << double( (clock2 - clock1 )) / CLOCKS_PER_SEC << "s"
  143.                   << " (" << accumulator << ")" << std::endl;
  144.    
  145.         accumulator = 0;
  146.         clock1 = clock();
  147.         for( int index = 0; index <= ITERATIONS; index++)
  148.             accumulator += inline_for_factorial( index % (FACTORIALCUT * i));
  149.         clock2 = clock();
  150.    
  151.         std::cout << "Inline for factorial: " << double( (clock2 - clock1 )) / CLOCKS_PER_SEC << "s"
  152.                   << " (" << accumulator << ")" << std::endl;
  153.     }
  154.  
  155.     std::cout << std::endl;
  156.  
  157.     system( "PAUSE");
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement