code_junkie

Should I use multiplication or division?

Nov 11th, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. y = x / 2.0;
  2. // or...
  3. y = x * 0.5;
  4.  
  5. time python -c 'for i in xrange(int(1e8)): t=12341234234.234 / 2.0'
  6. real 0m26.676s
  7. user 0m25.154s
  8. sys 0m0.076s
  9.  
  10. time python -c 'for i in xrange(int(1e8)): t=12341234234.234 * 0.5'
  11. real 0m17.932s
  12. user 0m16.481s
  13. sys 0m0.048s
  14.  
  15. time lua -e 'for i=1,1e8 do t=12341234234.234 / 2.0 end'
  16. real 0m7.956s
  17. user 0m7.332s
  18. sys 0m0.032s
  19.  
  20. time lua -e 'for i=1,1e8 do t=12341234234.234 * 0.5 end'
  21. real 0m7.997s
  22. user 0m7.516s
  23. sys 0m0.036s
  24.  
  25. time luajit -O -e 'for i=1,1e8 do t=12341234234.234 / 2.0 end'
  26. real 0m1.921s
  27. user 0m1.668s
  28. sys 0m0.004s
  29.  
  30. time luajit -O -e 'for i=1,1e8 do t=12341234234.234 * 0.5 end'
  31. real 0m1.843s
  32. user 0m1.676s
  33. sys 0m0.000s
  34.  
  35. y = x / 3.0;
  36. y = x * 0.333333; // how many 3's should there be, and how will the compiler round?
  37.  
  38. int main() {
  39.  
  40. volatile int a;
  41. volatile int b;
  42.  
  43. asm("## 5/2n");
  44. a = 5;
  45. a = a / 2;
  46.  
  47. asm("## 5*0.5");
  48. b = 5;
  49. b = b * 0.5;
  50.  
  51. asm("## done");
  52.  
  53. return a + b;
  54.  
  55. }
  56.  
  57. movl $5, -4(%ebp)
  58. movl -4(%ebp), %eax
  59. movl %eax, %edx
  60. shrl $31, %edx
  61. addl %edx, %eax
  62. sarl %eax
  63. movl %eax, -4(%ebp)
  64.  
  65. movl $5, -8(%ebp)
  66. movl -8(%ebp), %eax
  67. pushl %eax
  68. fildl (%esp)
  69. leal 4(%esp), %esp
  70. fmuls LC0
  71. fnstcw -10(%ebp)
  72. movzwl -10(%ebp), %eax
  73. orw $3072, %ax
  74. movw %ax, -12(%ebp)
  75. fldcw -12(%ebp)
  76. fistpl -16(%ebp)
  77. fldcw -10(%ebp)
  78. movl -16(%ebp), %eax
  79. movl %eax, -8(%ebp)
  80.  
  81. flds LC0
  82. fstl -8(%ebp)
  83. fldl -8(%ebp)
  84. flds LC1
  85. fmul %st, %st(1)
  86. fxch %st(1)
  87. fstpl -8(%ebp)
  88. fxch %st(1)
  89.  
  90. fstpl -16(%ebp)
  91. fldl -16(%ebp)
  92. fmulp %st, %st(1)
  93. fstpl -16(%ebp)
  94.  
  95. ## 5/2
  96.  
  97. ## 5*0.5
  98. ## done
  99.  
  100. movl $5, %eax
  101. leave
  102. ret
  103.  
  104. int y = 10;
  105. y = y >> 1;
  106. Console.WriteLine("value halved: " + y);
  107. y = y << 1;
  108. Console.WriteLine("now value doubled: " + y);
  109.  
  110. >>> for i in range(7):
  111. ... a=1/(10.0**i)
  112. ... b=(1/10.0)**i
  113. ... print i, a, b, a-b
  114. ...
  115. 0 1.0 1.0 0.0
  116. 1 0.1 0.1 0.0
  117. 2 0.01 0.01 -1.73472347598e-18
  118. 3 0.001 0.001 -2.16840434497e-19
  119. 4 0.0001 0.0001 -1.35525271561e-20
  120. 5 1e-05 1e-05 -1.69406589451e-21
  121. 6 1e-06 1e-06 -4.23516473627e-22
  122.  
  123. y = x * (1.0 / 2.0);
  124.  
  125. public void Mutiplication()
  126. {
  127. float a = 1.0f;
  128.  
  129. for(int i=0; i<1000000; i++)
  130. {
  131. a *= 0.5f;
  132. }
  133. }
  134. public void Division()
  135. {
  136. float a = 1.0f;
  137.  
  138. for(int i=0; i<1000000; i++)
  139. {
  140. a /= 2.0f;
  141. }
  142. }
  143.  
  144. Multiplications(): time/call: 1524.375 ms
  145. Division(): time/call: 1220.003 ms
Add Comment
Please, Sign In to add comment