Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2013
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <time.h>
  3. #include <chrono>
  4. #include <stdio.h>
  5. #include <iostream>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. static const int s_iArrayLen = 999999;
  11. static const int s_iMaxPipelineLen = 60;
  12. static const int s_iNumTrials = 10;
  13.  
  14.  
  15. int doWorkAndReturnMicrosecondsElapsed(int* vals, int pipelineLen)
  16. {
  17.     int* zeroNums = new int[pipelineLen];
  18.     int* oneNums = new int[pipelineLen];
  19.     for(int i = 0; i < pipelineLen; ++i)
  20.         zeroNums[i] = oneNums[i] = 0;
  21.  
  22.     chrono::time_point<chrono::system_clock> start, end;
  23.     start = chrono::system_clock::now();
  24.     for(int i = 0; i < s_iArrayLen; ++i)
  25.     {
  26.         if(vals[i] == 0)
  27.         {
  28.             for(int i = 0; i < pipelineLen; ++i)
  29.                 ++zeroNums[i];
  30.         }
  31.         else
  32.         {
  33.             for(int i = 0; i < pipelineLen; ++i)
  34.                 ++oneNums[i];
  35.         }
  36.     }
  37.     end = chrono::system_clock::now();
  38.     int elapsedMicroseconds = (int)chrono::duration_cast<chrono::microseconds>(end-start).count();
  39.  
  40.     //This should never fire, it just exists to guarantee the compiler doesn't compile out our zeroNums/oneNums
  41.     for(int i = 0; i < pipelineLen - 1; ++i)
  42.         if(zeroNums[i] != zeroNums[i+1] || oneNums[i] != oneNums[i+1])
  43.             return -1;
  44.  
  45.     delete[] zeroNums;
  46.     delete[] oneNums;
  47.  
  48.     return elapsedMicroseconds;
  49. }
  50.  
  51.  
  52. struct TestMethod
  53. {
  54.     string name;
  55.     void (*func)(int, int&);
  56.     int* results;
  57.  
  58.     TestMethod(string _name, void (*_func)(int, int&)) { name = _name; func = _func; results = new int[s_iMaxPipelineLen]; }
  59. };
  60.  
  61. int main()
  62. {
  63.     srand( (unsigned int)time(nullptr) );
  64.  
  65.     vector<TestMethod> testMethods;
  66.     testMethods.push_back(TestMethod("all-zero", [](int index, int& out) { out = 0; } ));
  67.     testMethods.push_back(TestMethod("repeat-0-1", [](int index, int& out) { out = index % 2; } ));
  68.     testMethods.push_back(TestMethod("repeat-0-0-0-1", [](int index, int& out) { out = (index % 4 == 0) ? 0 : 1; } ));
  69.     testMethods.push_back(TestMethod("rand", [](int index, int& out) { out = rand() % 2; } ));
  70.  
  71.  
  72.     int* vals = new int[s_iArrayLen];
  73.  
  74.     for(int currentPipelineLen = 0; currentPipelineLen < s_iMaxPipelineLen; ++currentPipelineLen)
  75.     {
  76.         for(int currentMethod = 0; currentMethod < (int)testMethods.size(); ++currentMethod)
  77.         {
  78.             int resultsSum = 0;
  79.             for(int trialNum = 0; trialNum < s_iNumTrials; ++trialNum)
  80.             {
  81.                 //Generate a new array...
  82.                 for(int i = 0; i < s_iArrayLen; ++i)
  83.                     testMethods[currentMethod].func(i, vals[i]);
  84.  
  85.                 //And record how long it takes
  86.                 resultsSum += doWorkAndReturnMicrosecondsElapsed(vals, currentPipelineLen);
  87.             }
  88.                
  89.             testMethods[currentMethod].results[currentPipelineLen] = (resultsSum / s_iNumTrials);
  90.         }
  91.     }
  92.  
  93.     cout << "\t";
  94.     for(int i = 0; i < s_iMaxPipelineLen; ++i)
  95.     {
  96.         cout << i << "\t";
  97.     }
  98.     cout << "\n";
  99.     for (int i = 0; i < (int)testMethods.size(); ++i)
  100.     {
  101.         cout << testMethods[i].name.c_str() << "\t";
  102.         for(int j = 0; j < s_iMaxPipelineLen; ++j)
  103.         {
  104.             cout << testMethods[i].results[j] << "\t";
  105.         }
  106.         cout << "\n";
  107.     }
  108.  
  109.     int end;
  110.     cin >> end;
  111.  
  112.     delete[] vals;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement