fork(18) 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 memmove() by inserting a small gap at the beginning of the buffer
  45. for (int stride = 2; stride <= MAXSTRIDE; stride *= 2) {
  46. Timer<high_resolution_clock> t;
  47. for (int i = 0; i < LOOPS; i++)
  48. memmove(b2, b1, BUFFERSIZE);
  49. double seconds = t.seconds();
  50. std::cout << "memmove (" << std::setfill('0') << std::setw(3)
  51. << stride << ") " << (seconds / LOOPS) << std::endl;
  52. }
  53.  
  54. // Test memcpy() by copying from buffer b1 to b2
  55. Timer<high_resolution_clock> t;
  56. for (int i = 0; i < LOOPS; i++)
  57. memcpy(b2, b1, BUFFERSIZE);
  58. double seconds = t.seconds();
  59. std::cout << "memcpy " << (seconds / LOOPS) << std::endl;
  60.  
  61. ::free(b1);
  62. ::free(b2);
  63. }
Success #stdin #stdout 4.64s 3140KB
stdin
Standard input is empty
stdout
memmove (002) 0.0610362
memmove (004) 0.0554264
memmove (008) 0.0575859
memmove (016) 0.057326
memmove (032) 0.0583542
memmove (064) 0.0561934
memmove (128) 0.0549391
memcpy 0.0537919