Advertisement
tyler569

C vs D

Apr 22nd, 2017
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. tyler@mac ~/Documents> gcc -O3 test.c -octest ; ./ctest
  2. C fast : 3010us
  3. C slow : 22876us
  4. tyler@mac ~/Documents> clang -O3 test.c -octest ; ./ctest
  5. C fast : 3297us
  6. C slow : 26991us
  7. tyler@mac ~/Documents> dmd -L-w -w -O -run test.d
  8. D together : 485us
  9. D idiomatic: 1267us
  10. D fast : 5071us
  11. D slow : 23423us
  12. tyler@mac ~/Documents> ldc2 -L-w -w -O -run test.d
  13. D together : 606us
  14. D idiomatic: 646us
  15. D fast : 692us
  16. D slow : 29892us
  17.  
  18.  
  19. ##########################################################################
  20. # test.c
  21.  
  22. #include <stdio.h>
  23. #include <time.h>
  24.  
  25. #define SIZE 1024
  26.  
  27. int arr[SIZE][SIZE];
  28.  
  29. void add_1_fast() {
  30. for (int i=0; i<SIZE; i++) {
  31. for (int j=0; j<SIZE; j++) {
  32. arr[i][j] += 1;
  33. }
  34. }
  35. }
  36.  
  37. void add_1_slow() {
  38. for (int i=0; i<SIZE; i++) {
  39. for (int j=0; j<SIZE; j++) {
  40. arr[j][i] += 1;
  41. }
  42. }
  43. }
  44.  
  45. int main() {
  46. clock_t x, y;
  47.  
  48. x = clock();
  49. add_1_fast();
  50. y = clock();
  51.  
  52. if (CLOCKS_PER_SEC == 1000000) {
  53. printf("C fast : %lius\n", y - x);
  54. } else {
  55. printf("C fast : %fus\n", (double)(y - x) / CLOCKS_PER_SEC * 1000000);
  56. }
  57.  
  58. x = clock();
  59. add_1_slow();
  60. y = clock();
  61.  
  62. if (CLOCKS_PER_SEC == 1000000) {
  63. printf("C slow : %lius\n", y - x);
  64. } else {
  65. printf("C slow : %fus\n", (double)(y - x) / CLOCKS_PER_SEC * 1000000);
  66. }
  67. }
  68.  
  69. ###################################################################################
  70. # test.d
  71.  
  72. import std.stdio;
  73. import std.datetime;
  74.  
  75. enum size = 1024;
  76.  
  77. int[size][size] arr;
  78.  
  79. auto add_1_together() {
  80. foreach (ref x; arr) {
  81. x[] += 1;
  82. }
  83. }
  84.  
  85. auto add_1_idiomatic() {
  86. foreach (ref x; arr) {
  87. foreach (ref y; x) {
  88. y += 1;
  89. }
  90. }
  91. }
  92.  
  93. auto add_1_fast() {
  94. for (int i=0; i<size; i++) {
  95. for (int j=0; j<size; j++) {
  96. arr[i][j] += 1;
  97. }
  98. }
  99. }
  100.  
  101. auto add_1_slow() {
  102. for (int i=0; i<size; i++) {
  103. for (int j=0; j<size; j++) {
  104. arr[j][i] += 1;
  105. }
  106. }
  107. }
  108.  
  109. void main() {
  110.  
  111. StopWatch sw1, sw2, sw3, sw4;
  112.  
  113. sw1.start();
  114. add_1_together();
  115. writeln("D together : ", sw1.peek().usecs, "us");
  116. sw1.stop();
  117.  
  118. sw2.start();
  119. add_1_idiomatic();
  120. writeln("D idiomatic: ", sw2.peek().usecs, "us");
  121. sw2.stop();
  122.  
  123. sw3.start();
  124. add_1_fast();
  125. writeln("D fast : ", sw3.peek().usecs, "us");
  126. sw3.stop();
  127.  
  128. sw4.start();
  129. add_1_slow();
  130. writeln("D slow : ", sw4.peek().usecs, "us");
  131. sw4.stop();
  132.  
  133. writeln(arr[45][199]);
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement