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 src[COUNT3], dst[COUNT3];
  30.  
  31. for (size_t i = 0; i < COUNT3; ++i) src[i] = (int)i;
  32.  
  33. // Do all benchmarks twice just in case
  34. benchmark_copy(src, dst, COUNT1, "COUNT1 (2^18)");
  35. benchmark_copy(src, dst, COUNT1, "COUNT1 (2^18)");
  36. benchmark_copy(src, dst, COUNT2, "COUNT2 (2^18 + 1)");
  37. benchmark_copy(src, dst, COUNT2, "COUNT2 (2^18 + 1)");
  38. benchmark_copy(src, dst, COUNT3, "COUNT3 (2^18 + 10)");
  39. benchmark_copy(src, dst, COUNT3, "COUNT3 (2^18 + 10)");
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 3.36s 5312KB
stdin
Standard input is empty
stdout
COUNT1 (2^18): Copied 1048576 bytes (262144 ints) 10000 times
COUNT1 (2^18): Total time: 0.569267 sec
14 
COUNT1 (2^18): Copied 1048576 bytes (262144 ints) 10000 times
COUNT1 (2^18): Total time: 0.558774 sec
14 
COUNT2 (2^18 + 1): Copied 1048580 bytes (262145 ints) 10000 times
COUNT2 (2^18 + 1): Total time: 0.560326 sec
14 
COUNT2 (2^18 + 1): Copied 1048580 bytes (262145 ints) 10000 times
COUNT2 (2^18 + 1): Total time: 0.554844 sec
14 
COUNT3 (2^18 + 10): Copied 1048616 bytes (262154 ints) 10000 times
COUNT3 (2^18 + 10): Total time: 0.544589 sec
14 
COUNT3 (2^18 + 10): Copied 1048616 bytes (262154 ints) 10000 times
COUNT3 (2^18 + 10): Total time: 0.565179 sec
14