code_junkie

Why are compilers so stupid?

Nov 13th, 2011
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. int x = 0;
  2. for (int i = 0; i < 100 * 1000 * 1000 * 1000; ++i) {
  3. x += x + x + x + x + x;
  4. }
  5. System.out.println(x);
  6.  
  7. public int slow() {
  8. String s = "x";
  9. for (int i = 0; i < 100000; ++i) {
  10. s += "x";
  11. }
  12. return 10;
  13. }
  14.  
  15. #include <stdio.h> /* printf() */
  16.  
  17. int factorial(int n) {
  18. return n == 0 ? 1 : n * factorial(n - 1);
  19. }
  20.  
  21. int main() {
  22. int n = 10;
  23.  
  24. printf("factorial(%d) = %dn", n, factorial(n));
  25.  
  26. return 0;
  27. }
  28.  
  29. factorial:
  30. .LFB13:
  31. testl %edi, %edi
  32. movl $1, %eax
  33. je .L3
  34. .p2align 4,,10
  35. .p2align 3
  36. .L4:
  37. imull %edi, %eax
  38. subl $1, %edi
  39. jne .L4
  40. .L3:
  41. rep
  42. ret
  43.  
  44. main:
  45. .LFB14:
  46. subq $8, %rsp
  47. .LCFI0:
  48. movl $3628800, %edx
  49. movl $10, %esi
  50. movl $.LC0, %edi
  51. xorl %eax, %eax
  52. call printf
  53. xorl %eax, %eax
  54. addq $8, %rsp
  55. ret
  56.  
  57. x = 0
  58. sleep 6 // Let's assume this is defined somewhere.
  59. print x
  60.  
  61. int x = 1;
  62. int y = 1;
  63. int z = x - y;
  64. for (int i = 0; i < 100 * 1000 * 1000 * 1000; ++i) {
  65. z += z + z + z + z + z;
  66. }
  67. System.out.println(z);
  68.  
  69. extern int x; // defined elsewhere
  70. for (int i = 0; i < 100 * 1000 * 1000 * 1000; ++i) {
  71. x += x + x + x + x + x;
  72. }
  73. return x;
  74.  
  75. extern volatile int x; // defined elsewhere
  76. for (int i = 0; i < 100 * 1000 * 1000 * 1000; ++i) {
  77. x += x + x + x + x + x;
  78. }
  79. return x;
  80.  
  81. [js@HOST2 java]$ gcj --main=Optimize -O2 Optimize.java
  82. [js@HOST2 java]$ ./a.out
  83. 0
  84. [js@HOST2 java]$
  85.  
  86. class Optimize {
  87. private static int doIt() {
  88. int x = 0;
  89. for (int i = 0; i < 100 * 1000 * 1000 * 1000; ++i) {
  90. x += x + x + x + x + x;
  91. }
  92. return x;
  93. }
  94. public static void main(String[] args) {
  95. for(int i=0;i<5;i++) {
  96. doIt();
  97. }
  98. }
  99. }
  100.  
  101. 1 java.lang.String::hashCode (60 bytes)
  102. 1% Optimize::doIt @ 4 (30 bytes)
  103. 2 Optimize::doIt (30 bytes)
  104.  
  105. "starting windows"
  106. "enjoy freecell/solitaire"
  107. "shutting down windows"
  108.  
  109. #include <stdio.h>
  110. int main()
  111. {
  112. int x = 0;
  113. for (int i = 0; i < 100 * 1000 * 1000 * 1000; ++i) {
  114. x += x + x + x + x + x;
  115. }
  116. printf("%d", x);
  117. }
Add Comment
Please, Sign In to add comment