Advertisement
Guest User

MemBenchmark.cpp

a guest
May 29th, 2012
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. // BlogBenchmarks.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <iostream>
  7. #include <ctime>
  8. #include <vector>
  9. #include <list>
  10. #include <utility>
  11. #include <string>
  12.  
  13. using namespace std;
  14.  
  15. struct benchmark
  16. {
  17.     benchmark(const wchar_t* message)
  18.         : m_msg(message)
  19.     {
  20.         m_start = time(NULL);
  21.         std::wcout << m_msg << L" started" << std::endl;
  22.     }
  23.  
  24.     ~benchmark()
  25.     {
  26.         std::wcout << m_msg << L": " << time(NULL) - m_start << L" sec" << std::endl;
  27.     }
  28.  
  29.     time_t m_start;
  30.     const wchar_t* m_msg;
  31. };
  32.  
  33.  
  34. const size_t totalSize = 100000000; // 100 million bytes
  35. void allocs_1()
  36. {
  37.     for (int i = 0; i < totalSize / 4; i++)
  38.         new int;
  39. }
  40.  
  41. void allocs_2()
  42. {
  43.     for (int i = 0; i < totalSize / 40; i++)
  44.         new int[10];
  45. }
  46.  
  47. void allocs_3()
  48. {
  49.     for (int i = 0; i < totalSize / 400; i++)
  50.         new int[100];
  51.  
  52. }
  53.  
  54. void allocs_4()
  55. {
  56.     for (int i = 0; i < totalSize / 4000; i++)
  57.         new int[1000];
  58. }
  59.  
  60. void allocs_5()
  61. {
  62.     for (int i = 0; i < totalSize / 40000; i++)
  63.         new int[10000];
  64. }
  65.  
  66. int main(int argc, char* argv[])
  67. {
  68.     std::string dummy;
  69.     std::wcout << L"Press Enter to start" << std::endl;
  70.     std::getline(std::cin, dummy);
  71.  
  72.     allocs_1();
  73.     std::wcout << L"Allocs 1 completed" << std::endl;
  74.     std::getline(std::cin, dummy);
  75.  
  76.     allocs_2();
  77.     std::wcout << L"Allocs 2 completed" << std::endl;
  78.     std::getline(std::cin, dummy);
  79.  
  80.     allocs_3();
  81.     std::wcout << L"Allocs 3 completed" << std::endl;
  82.     std::getline(std::cin, dummy);
  83.  
  84.     allocs_4();
  85.     std::wcout << L"Allocs 4 completed" << std::endl;
  86.     std::getline(std::cin, dummy);
  87.  
  88.     allocs_5();
  89.     std::wcout << L"Allocs 5 completed" << std::endl;
  90.     std::getline(std::cin, dummy);
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement