Advertisement
Guest User

Untitled

a guest
May 31st, 2012
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <ctime>
  4.  
  5. #define NON_ZERO
  6. //#define ZERO
  7.  
  8. #ifdef NON_ZERO
  9. #define FUNCTION1 nonZero1
  10. #define FUNCTION2 nonZero2
  11. #define FILL 0
  12. #endif
  13.  
  14. #ifdef ZERO
  15. #define FUNCTION1 zero1
  16. #define FUNCTION2 zero2
  17. #define FILL 2000
  18. #endif
  19.  
  20. bool nonZero1(const int integer);
  21. bool nonZero2(const int integer);
  22. bool zero1(const int integer);
  23. bool zero2(const int integer);
  24. unsigned int test1(const unsigned int size, const int* const buffer);
  25. unsigned int test2(const unsigned int size, const int* const buffer);
  26. void benchmark1(const unsigned int size, const int* const buffer);
  27. void benchmark2(const unsigned int size, const int* const buffer);
  28.  
  29. void benchmark1(const unsigned int size, const int* const buffer)
  30. {
  31.     unsigned long long result = 0;
  32.     const std::clock_t startClocks = std::clock();
  33.  
  34.     for (unsigned int i = 0; i < 10; ++i)
  35.     {
  36.         result += test1(size, buffer);
  37.     }
  38.    
  39.     const std::clock_t clocksTaken = std::clock() - startClocks;
  40.     std::cout << "Benchmark1. Result: " << result << ". Clocks taken: " << clocksTaken << "." << std::endl;
  41. }
  42.  
  43. void benchmark2(const unsigned int size, const int* const buffer)
  44. {
  45.     unsigned long long result = 0;
  46.     const std::clock_t startClocks = std::clock();
  47.  
  48.     for (unsigned int i = 0; i < 10; ++i)
  49.     {
  50.         result += test2(size, buffer);
  51.     }
  52.    
  53.     const std::clock_t clocksTaken = std::clock() - startClocks;
  54.     std::cout << "Benchmark2. Result: " << result << ". Clocks taken: " << clocksTaken << "." << std::endl;
  55. }
  56.  
  57. int main(void)
  58. {
  59.     const unsigned int size = 200000000;
  60.     int* buffer = new int[size];
  61.     std::fill(buffer, buffer + size, FILL);
  62.  
  63.     for (unsigned int i = 0; i < 4; i++)
  64.     {
  65.         benchmark1(size, buffer);
  66.         benchmark2(size, buffer);
  67.         std::cout << std::endl;
  68.     }
  69.  
  70.     delete[] buffer; buffer = nullptr;
  71.  
  72.     std::cin.sync();
  73.     std::cin.get();
  74.     return EXIT_SUCCESS;
  75. }
  76.  
  77. bool nonZero1(const int integer)
  78. {
  79.     bool notZero1 = integer != 0;
  80.  
  81.     return notZero1;
  82. }
  83.  
  84. bool nonZero2(const int integer)
  85. {
  86.     bool notZero2 = integer < 0 || integer > 0;
  87.  
  88.     return notZero2;
  89. }
  90.  
  91. bool zero1(const int integer)
  92. {
  93.     bool zero1 = integer == 0;
  94.  
  95.     return zero1;
  96. }
  97.  
  98. bool zero2(const int integer)
  99. {
  100.     bool zero2 = !(integer < 0 || integer > 0);
  101.  
  102.     return zero2;
  103. }
  104.  
  105. unsigned int test1(const unsigned int size, const int* const buffer)
  106. {
  107.     unsigned int result = 0;
  108.  
  109.     for (unsigned int i = 0; i < size; ++i)
  110.     {
  111.         if (FUNCTION1(buffer[i]))
  112.         {
  113.             ++result;
  114.         }
  115.     }
  116.  
  117.     return result;
  118. }
  119.  
  120. unsigned int test2(const unsigned int size, const int* const buffer)
  121. {
  122.     unsigned int result = 0;
  123.  
  124.     for (unsigned int i = 0; i < size; ++i)
  125.     {
  126.         if (FUNCTION2(buffer[i]))
  127.         {
  128.             ++result;
  129.         }
  130.     }
  131.  
  132.     return result;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement