fork(1) download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <chrono>
  4.  
  5. // 8Kb of space.
  6. char smallSpace[8 * 1024];
  7.  
  8. // 64Mb of space (larger than cache)
  9. char bigSpace[64 * 1024 * 1024];
  10.  
  11. void populateSpaces(char val)
  12. {
  13. memset(smallSpace, val, sizeof(smallSpace));
  14. memset(bigSpace, val, sizeof(bigSpace));
  15. std::cout << "Populated spaces" << std::endl;
  16. }
  17.  
  18. unsigned int doWork(char* ptr, size_t size)
  19. {
  20. unsigned int total = 0;
  21. const char* end = ptr + size;
  22. while (ptr < end) {
  23. total += *(ptr++);
  24. }
  25. return total;
  26. }
  27.  
  28. unsigned int strideWork(char* ptr, size_t size)
  29. {
  30. unsigned int total = 0;
  31. const char* end = ptr + size;
  32. const size_t StrideRate = 1024;
  33. for (size_t i = 0; i < StrideRate; ++i, ++ptr) {
  34. while (ptr < end) {
  35. total += (*ptr++);
  36. }
  37. }
  38. return total;
  39. }
  40.  
  41. using namespace std;
  42. using namespace chrono;
  43.  
  44. void doTiming(unsigned int (*function)(char*, size_t), const char* label, char* ptr, size_t size)
  45. {
  46. cout << label << ": ";
  47. const high_resolution_clock::time_point start = high_resolution_clock::now();
  48. auto result = function(ptr, size);
  49. const high_resolution_clock::time_point stop = high_resolution_clock::now();
  50. auto delta = duration_cast<nanoseconds>(stop - start).count();
  51. cout << "took " << delta << "ns (result is " << result << ")" << endl;
  52. }
  53.  
  54. int main()
  55. {
  56. cout << "Timer resultion is " <<
  57. duration_cast<nanoseconds>(high_resolution_clock::duration(1)).count()
  58. << "ns" << endl;
  59.  
  60. populateSpaces(1);
  61.  
  62. doTiming(doWork, "doWork/small", smallSpace, sizeof(smallSpace));
  63. doTiming(doWork, "doWork/small", smallSpace, sizeof(smallSpace));
  64. doTiming(doWork, "doWork/small", smallSpace, sizeof(smallSpace));
  65. doTiming(doWork, "doWork/big", bigSpace, sizeof(bigSpace));
  66. doTiming(doWork, "doWork/big", bigSpace, sizeof(bigSpace));
  67. doTiming(doWork, "doWork/small", smallSpace, sizeof(smallSpace));
  68. doTiming(doWork, "doWork/small", smallSpace, sizeof(smallSpace));
  69. doTiming(doWork, "doWork/small", smallSpace, sizeof(smallSpace));
  70.  
  71. populateSpaces(2);
  72.  
  73. doTiming(strideWork, "strideWork/small", smallSpace, sizeof(smallSpace));
  74. doTiming(strideWork, "strideWork/small", smallSpace, sizeof(smallSpace));
  75. doTiming(strideWork, "strideWork/small", smallSpace, sizeof(smallSpace));
  76. doTiming(strideWork, "strideWork/big", bigSpace, sizeof(bigSpace));
  77. doTiming(strideWork, "strideWork/big", bigSpace, sizeof(bigSpace));
  78. }
Success #stdin #stdout 0.33s 68864KB
stdin
Standard input is empty
stdout
Timer resultion is 1ns
Populated spaces
doWork/small: took 8384ns (result is 8192)
doWork/small: took 7702ns (result is 8192)
doWork/small: took 7686ns (result is 8192)
doWork/big: took 64921206ns (result is 67108864)
doWork/big: took 65120677ns (result is 67108864)
doWork/small: took 8237ns (result is 8192)
doWork/small: took 7678ns (result is 8192)
doWork/small: took 7677ns (result is 8192)
Populated spaces
strideWork/small: took 10112ns (result is 16384)
strideWork/small: took 9570ns (result is 16384)
strideWork/small: took 9559ns (result is 16384)
strideWork/big: took 65512138ns (result is 134217728)
strideWork/big: took 65005505ns (result is 134217728)