fork(3) download
  1. #include <string.h>
  2.  
  3. #include <iostream>
  4. #include <chrono>
  5. #include <iostream>
  6. #include <iomanip>
  7.  
  8. using namespace std::chrono;
  9.  
  10. // based on
  11. // http://w...content-available-to-author-only...t.org/doc/libs/1_54_0/libs/chrono/example/await_keystroke.cpp
  12. template<class Clock>
  13. class Timer
  14. {
  15. typename Clock::time_point start;
  16.  
  17. public:
  18. Timer()
  19. : start(Clock::now()) {
  20. }
  21.  
  22. typename Clock::duration elapsed() const {
  23. return (Clock::now() - start);
  24. }
  25.  
  26. double seconds() const {
  27. return (elapsed().count() *
  28. ((double)Clock::period::num / Clock::period::den));
  29. }
  30. };
  31.  
  32. #define BUFFERSIZE (100 * 1024 * 1024) // 100 mb
  33. #define LOOPS 10
  34. #define MAXSTRIDE 128
  35.  
  36. int main()
  37. {
  38. char *b1 = (char *)::malloc(BUFFERSIZE + MAXSTRIDE);
  39. char *b2 = (char *)::malloc(BUFFERSIZE + MAXSTRIDE);
  40. ::memset(b1, 0, BUFFERSIZE + MAXSTRIDE);
  41.  
  42. // Each test is performed |LOOP| times; the average run-time is then printed.
  43.  
  44. // Test memcpy() by copying from buffer b1 to b2
  45. {Timer<high_resolution_clock> t;
  46. for (int i = 0; i < LOOPS; i++)
  47. memcpy(b2, b1, BUFFERSIZE);
  48. double seconds = t.seconds();
  49. std::cout << "memcpy " << (seconds / LOOPS) << std::endl;
  50. }
  51. // Test memcpy() by copying from buffer b1 to b2
  52. Timer<high_resolution_clock> t;
  53. for (int i = 0; i < LOOPS; i++)
  54. memcpy(b2, b1, BUFFERSIZE);
  55. double seconds = t.seconds();
  56. std::cout << "memcpy " << (seconds / LOOPS) << std::endl;
  57.  
  58. // Test memmove() by inserting a small gap at the beginning of the buffer
  59. for (int stride = 2; stride <= MAXSTRIDE; stride *= 2) {
  60. Timer<high_resolution_clock> t;
  61. for (int i = 0; i < LOOPS; i++)
  62. memmove(b2, b1, BUFFERSIZE);
  63. double seconds = t.seconds();
  64. std::cout << "memmove (" << std::setfill('0') << std::setw(3)
  65. << stride << ") " << (seconds / LOOPS) << std::endl;
  66. }
  67.  
  68. ::free(b1);
  69. ::free(b2);
  70. }
Time limit exceeded #stdin #stdout 5s 207936KB
stdin
Standard input is empty
stdout
memcpy 0.0688002
memcpy 0.0583162
memmove (002) 0.0577443
memmove (004) 0.05862
memmove (008) 0.0601029
memmove (016) 0.0601265
memmove (032) 0.0592741
memmove (064) 0.0604098