fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #define COUNT1 262144 // 2^18
  7. #define COUNT2 262145 // 2^18 + 1
  8. #define COUNT3 262154 // 2^18 + 10
  9. #define REPEATS 10000
  10.  
  11. void benchmark_copy(const int *src, int *dst, size_t count, const char *label) {
  12. size_t size_bytes = count * sizeof(int);
  13.  
  14. clock_t start = clock();
  15. for (int i = 0; i < REPEATS; ++i) {
  16. memcpy(dst, src, size_bytes);
  17. }
  18. clock_t end = clock();
  19.  
  20. double total_sec = (double)(end - start) / CLOCKS_PER_SEC;
  21. printf("%s: Copied %zu bytes (%zu ints) %d times\n", label, size_bytes, count, REPEATS);
  22. printf("%s: Total time: %.6f sec\n", label, total_sec);
  23.  
  24. // Prevent optimizing away some stuff
  25. printf("%d \n", dst[14]);
  26. }
  27.  
  28. int main(void) {
  29. static int src1[COUNT1], dst1[COUNT1];
  30. static int src2[COUNT2], dst2[COUNT2];
  31. static int src3[COUNT3], dst3[COUNT3];
  32.  
  33. for (size_t i = 0; i < COUNT1; ++i) src1[i] = (int)i;
  34. for (size_t i = 0; i < COUNT2; ++i) src2[i] = (int)i;
  35. for (size_t i = 0; i < COUNT3; ++i) src3[i] = (int)i;
  36.  
  37. // Do all benchmarks twice just in case
  38. benchmark_copy(src1, dst1, COUNT1, "COUNT1 (2^18)");
  39. benchmark_copy(src1, dst1, COUNT1, "COUNT1 (2^18)");
  40. benchmark_copy(src2, dst2, COUNT2, "COUNT2 (2^18 + 1)");
  41. benchmark_copy(src2, dst2, COUNT2, "COUNT2 (2^18 + 1)");
  42. benchmark_copy(src3, dst3, COUNT3, "COUNT3 (2^18 + 10)");
  43. benchmark_copy(src3, dst3, COUNT3, "COUNT3 (2^18 + 10)");
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 3.39s 7744KB
stdin
Standard input is empty
stdout
COUNT1 (2^18): Copied 1048576 bytes (262144 ints) 10000 times
COUNT1 (2^18): Total time: 0.570040 sec
14 
COUNT1 (2^18): Copied 1048576 bytes (262144 ints) 10000 times
COUNT1 (2^18): Total time: 0.560664 sec
14 
COUNT2 (2^18 + 1): Copied 1048580 bytes (262145 ints) 10000 times
COUNT2 (2^18 + 1): Total time: 0.552398 sec
14 
COUNT2 (2^18 + 1): Copied 1048580 bytes (262145 ints) 10000 times
COUNT2 (2^18 + 1): Total time: 0.565915 sec
14 
COUNT3 (2^18 + 10): Copied 1048616 bytes (262154 ints) 10000 times
COUNT3 (2^18 + 10): Total time: 0.568258 sec
14 
COUNT3 (2^18 + 10): Copied 1048616 bytes (262154 ints) 10000 times
COUNT3 (2^18 + 10): Total time: 0.563707 sec
14