fork(6) 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() {
  18.  
  19. const char *str1 = "balls!";
  20. char str2[20];
  21.  
  22.  
  23. auto start = chrono::steady_clock::now();
  24.  
  25. for (int i = 0; i < 10000; ++i) {
  26.  
  27. volatile int len = strcpylen(str2, str1);
  28. }
  29.  
  30. auto end = chrono::steady_clock::now();
  31.  
  32. auto diff = end - start;
  33.  
  34. cout << "part1: " << chrono::duration <double, nano> (diff).count() << endl;
  35.  
  36. start = chrono::steady_clock::now();
  37.  
  38. for (int i = 0; i < 10000; ++i) {
  39.  
  40. volatile int len;
  41. strcpy(str2, str1);
  42. len = strlen(str2);
  43. }
  44.  
  45. end = chrono::steady_clock::now();
  46.  
  47. diff = end - start;
  48.  
  49. cout << "part2: " << chrono::duration <double, nano> (diff).count() << endl;
  50. // your code goes here
  51. return 0;
  52. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
part1: 65535
part2: 10404