Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. // Copyright © 2017 Martin Ueding <dev@martin-ueding.de>
  2. // Licensed under the MIT license
  3.  
  4. // Compiled with:
  5. //
  6. // gcc -Wall -Wpedantic -fopenmp strcpy.c -o strcpy -O3 -g
  7.  
  8. /*
  9. String length: 1,000,000,000
  10. short_strcpy: 0.961368
  11. short_strcpy: 0.709614
  12. longer_strcpy: 0.695906
  13. longer_strcpy: 0.698939
  14. other_strcpy: 0.698368
  15. other_strcpy: 0.693769
  16. another_strcpy: 0.708977
  17. another_strcpy: 0.711919
  18. strcpy: 0.198001
  19. strcpy: 0.205245
  20. memcpy: 6.49998e-08
  21. memcpy: 6.00012e-08
  22.  
  23. String length: 100,000,000
  24. short_strcpy: 0.119037
  25. short_strcpy: 0.0705183
  26. longer_strcpy: 0.0690453
  27. longer_strcpy: 0.0691149
  28. other_strcpy: 0.070922
  29. other_strcpy: 0.0715886
  30. another_strcpy: 0.0706077
  31. another_strcpy: 0.0704344
  32. strcpy: 0.0207186
  33. strcpy: 0.0242169
  34. memcpy: 5.10008e-08
  35. memcpy: 6.0998e-08
  36.  
  37. String length: 10,000,000
  38. short_strcpy: 0.0218649
  39. short_strcpy: 0.0138642
  40. longer_strcpy: 0.0113662
  41. longer_strcpy: 0.0105511
  42. other_strcpy: 0.0101318
  43. other_strcpy: 0.00947807
  44. another_strcpy: 0.00931944
  45. another_strcpy: 0.00907402
  46. strcpy: 0.00201328
  47. strcpy: 0.00325
  48. memcpy: 6.60002e-08
  49. memcpy: 5.4999e-08
  50.  
  51. String length: 1,000,000
  52. short_strcpy: 0.00257647
  53. short_strcpy: 0.00201034
  54. longer_strcpy: 0.00175444
  55. longer_strcpy: 0.00172597
  56. other_strcpy: 0.00174712
  57. other_strcpy: 0.00175475
  58. another_strcpy: 0.00177107
  59. another_strcpy: 0.0017567
  60. strcpy: 0.000258751
  61. strcpy: 0.000230961
  62. memcpy: 1.06e-07
  63. memcpy: 9.19972e-08
  64.  
  65. String length: 100,000
  66. short_strcpy: 0.000362797
  67. short_strcpy: 0.000261451
  68. longer_strcpy: 0.000261319
  69. longer_strcpy: 0.000293498
  70. other_strcpy: 0.00026597
  71. other_strcpy: 0.000260578
  72. another_strcpy: 0.000280667
  73. another_strcpy: 0.000274899
  74. strcpy: 2.7554e-05
  75. strcpy: 1.5588e-05
  76. memcpy: 1.12999e-07
  77. memcpy: 1.20002e-07
  78. */
  79.  
  80. #include <omp.h>
  81.  
  82. #include <stdio.h>
  83. #include <stdlib.h>
  84. #include <string.h>
  85.  
  86. #define LEN 10000000UL
  87.  
  88. void short_strcpy(char *d, char *s) {
  89. while ((*d++ = *s++));
  90. }
  91.  
  92. void longer_strcpy(char *d, char *s) {
  93. size_t i = 0;
  94. while (s[i] != '\0') {
  95. d[i] = s[i];
  96. i++;
  97. }
  98. d[i] = s[i];
  99. }
  100.  
  101. void other_strcpy(char *d, char *s) {
  102. size_t i = 0;
  103. while ((d[i] = s[i]) != '\0') {
  104. i++;
  105. }
  106. }
  107.  
  108. void another_strcpy(char *d, char *s) {
  109. while ((*d++ = *s++) != '\0');
  110. }
  111.  
  112. int main(int argc, char **argv) {
  113. char *s = malloc(LEN);
  114. char *d = malloc(LEN);
  115.  
  116. double start, end;
  117.  
  118. printf("String length: %ld\n", LEN);
  119.  
  120. memset(s, 1, LEN);
  121. s[LEN - 1] = 0;
  122.  
  123. // Warm the caches.
  124. start = omp_get_wtime();
  125. short_strcpy(d, s);
  126. end = omp_get_wtime();
  127. printf("short_strcpy: %g\n", end - start);
  128.  
  129. start = omp_get_wtime();
  130. short_strcpy(d, s);
  131. end = omp_get_wtime();
  132. printf("short_strcpy: %g\n", end - start);
  133.  
  134. start = omp_get_wtime();
  135. longer_strcpy(d, s);
  136. end = omp_get_wtime();
  137. printf("longer_strcpy: %g\n", end - start);
  138.  
  139. start = omp_get_wtime();
  140. longer_strcpy(d, s);
  141. end = omp_get_wtime();
  142. printf("longer_strcpy: %g\n", end - start);
  143.  
  144. start = omp_get_wtime();
  145. other_strcpy(d, s);
  146. end = omp_get_wtime();
  147. printf("other_strcpy: %g\n", end - start);
  148.  
  149. start = omp_get_wtime();
  150. other_strcpy(d, s);
  151. end = omp_get_wtime();
  152. printf("other_strcpy: %g\n", end - start);
  153.  
  154. start = omp_get_wtime();
  155. another_strcpy(d, s);
  156. end = omp_get_wtime();
  157. printf("another_strcpy: %g\n", end - start);
  158.  
  159. start = omp_get_wtime();
  160. another_strcpy(d, s);
  161. end = omp_get_wtime();
  162. printf("another_strcpy: %g\n", end - start);
  163.  
  164. start = omp_get_wtime();
  165. strcpy(d, s);
  166. end = omp_get_wtime();
  167. printf("strcpy: %g\n", end - start);
  168.  
  169. start = omp_get_wtime();
  170. strcpy(d, s);
  171. end = omp_get_wtime();
  172. printf("strcpy: %g\n", end - start);
  173.  
  174. start = omp_get_wtime();
  175. memcpy(d, s, LEN);
  176. end = omp_get_wtime();
  177. printf("memcpy: %g\n", end - start);
  178.  
  179. start = omp_get_wtime();
  180. memcpy(d, s, LEN);
  181. end = omp_get_wtime();
  182. printf("memcpy: %g\n", end - start);
  183.  
  184. return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement