Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.39 KB | None | 0 0
  1. #include <stack>
  2. #include <vector>
  3. #include <iostream>
  4. #include <cstdlib>
  5.  
  6. #include <sys/time.h>
  7.  
  8. #define LOOPNUM 1000000
  9.  
  10. // LOOPTEST to be defined with value standardCPP or standardC
  11.  
  12.  
  13. void standardVect(std::vector<int>& vect)
  14. {
  15.     /* because we know ;) */
  16.     vect.reserve(LOOPNUM);
  17.     for (int i=0; i < LOOPNUM; i++)
  18.     {
  19.         vect.push_back(i);
  20.     }
  21. }
  22.  
  23.  
  24.  
  25. void standardStack (std::stack<int>& stack)
  26. {
  27.     for (int i=0; i < LOOPNUM; i++)
  28.     {
  29.         stack.push(i);
  30.     }
  31. }
  32.  
  33. struct stacK
  34. {
  35.     struct stacK* previous;
  36.     int i;
  37. };
  38.  
  39. void standardC (stacK* head)
  40. {
  41.     stacK *current (head);
  42.     for (int i=0; i < LOOPNUM; i++)
  43.     {
  44.         if (stacK *tmp = ( struct stacK * ) malloc ( sizeof ( struct stacK ) ))
  45.         {
  46.             tmp ->i = i;
  47.             tmp ->previous = current;
  48.             current = tmp;
  49.         }
  50.     }
  51. };
  52.  
  53. int main()
  54. {
  55.     std::stack<int> stack;
  56.     std::vector<int> vect;
  57.     stacK stackC;
  58.  
  59.     timespec start, end;
  60.  
  61.  
  62.     clock_gettime(CLOCK_MONOTONIC, &start);
  63.     #if BENCH_C
  64.     standardC(&stackC);
  65.     #endif
  66.  
  67.     #if BENCH_STACK
  68.     standardStack(stack);
  69.     #endif
  70.  
  71.     #if BENCH_VECT
  72.     standardVect(vect);
  73.     #endif
  74.     clock_gettime(CLOCK_MONOTONIC, &end);
  75.  
  76.     std::cout << (end.tv_sec - start.tv_sec) * 1000 * 1000 * 1000ULL + (end.tv_nsec - start.tv_nsec) << std::endl;
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement