fork download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <chrono>
  4. #include <cstring>
  5. using namespace std;
  6.  
  7.  
  8. unsigned int strcpylen(char dest[], const char source[]) {
  9. unsigned int i = 0;
  10. while ((dest[i] = source[i]) != '\0') {
  11. i++;
  12. }
  13. return i;
  14. }
  15.  
  16.  
  17. int main(int argc, char** argv) {
  18. char str1[20];
  19. scanf("%s", str1);
  20.  
  21. char str2[20];
  22.  
  23.  
  24. auto start = chrono::steady_clock::now();
  25.  
  26. for (int i = 0; i < 10000; ++i) {
  27.  
  28. volatile int len = strcpylen(str2, str1);
  29. }
  30.  
  31. auto end = chrono::steady_clock::now();
  32.  
  33. auto diff = end - start;
  34.  
  35. cout << "part1: " << chrono::duration <double, nano> (diff).count() << endl;
  36.  
  37. start = chrono::steady_clock::now();
  38.  
  39. for (int i = 0; i < 10000; ++i) {
  40.  
  41. volatile int len;
  42. strcpy(str2, str1);
  43. len = strlen(str2);
  44. }
  45.  
  46. end = chrono::steady_clock::now();
  47.  
  48. diff = end - start;
  49.  
  50. cout << "part2: " << chrono::duration <double, nano> (diff).count() << endl;
  51. // your code goes here
  52. return 0;
  53. }
Success #stdin #stdout 0s 3472KB
stdin
balls!
stdout
part1: 33858
part2: 63196