Guest User

Untitled

a guest
Feb 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. There's a better way to run that benchmark - using the `benchmark-ips` tool which is designed carefully to allow for optimising implementations of Ruby to do their work and will allow for the JIT to warm up.
  2.  
  3. ```ruby
  4. require 'benchmark/ips'
  5.  
  6. def calculate(a, b, n = 40_000_000)
  7. i = 0
  8. c = 0
  9. while i < n
  10. a = a * 16807 % 2147483647
  11. b = b * 48271 % 2147483647
  12.  
  13. c += 1 if (a & 0xffff) == (b & 0xffff)
  14. i += 1
  15. end
  16. c
  17. end
  18.  
  19. Benchmark.ips do |x|
  20. x.iterations = 3
  21.  
  22. x.report("calculate") do |times|
  23. calculate(65, 8921, 100_000)
  24. end
  25. end
  26.  
  27. raise unless calculate(65, 8921) == 588
  28. ```
  29.  
  30. ```
  31. ruby 2.6.0dev (2018-02-07 trunk 62270) [x86_64-darwin17]
  32. Warming up --------------------------------------
  33. calculate 9.000 i/100ms
  34. calculate 9.000 i/100ms
  35. calculate 9.000 i/100ms
  36. Calculating -------------------------------------
  37. calculate 893.647 (±10.3%) i/s - 4.419k in 5.003980s
  38. calculate 872.846 (±12.7%) i/s - 4.275k in 5.000368s
  39. calculate 934.028 (±10.1%) i/s - 4.599k in 5.002686s
  40.  
  41. ruby 2.6.0dev (2018-02-07 trunk 62270) [x86_64-darwin17] (+JIT)
  42. Warming up --------------------------------------
  43. calculate 10.000 i/100ms
  44. calculate 18.000 i/100ms
  45. calculate 18.000 i/100ms
  46. Calculating -------------------------------------
  47. calculate 3.174k (± 9.9%) i/s - 15.552k in 4.999483s
  48. calculate 3.180k (± 8.2%) i/s - 15.786k in 5.002768s
  49. calculate 3.085k (±14.8%) i/s - 14.958k in 5.002559s
  50.  
  51. rubinius 3.84 (2.3.1 3970f17d 2017-08-02 4.0.1) [x86_64-darwin17.0.0]
  52. Warming up --------------------------------------
  53. calculate 3.000 i/100ms
  54. calculate 3.000 i/100ms
  55. calculate 3.000 i/100ms
  56. Calculating -------------------------------------
  57. calculate 114.830 (± 8.7%) i/s - 570.000
  58. calculate 114.671 (± 9.6%) i/s - 570.000
  59. calculate 118.078 (± 7.6%) i/s - 588.000
  60.  
  61. jruby 9.1.13.0 (2.3.3) 2017-09-06 8e1c115 Java HotSpot(TM) 64-Bit Server VM 25.144-b01 on 1.8.0_144-b01 +jit [darwin-x86_64]
  62. Warming up --------------------------------------
  63. calculate 9.000 i/100ms
  64. calculate 10.000 i/100ms
  65. calculate 10.000 i/100ms
  66. Calculating -------------------------------------
  67. calculate 1.083k (±11.0%) i/s - 5.330k in 4.983511s
  68. calculate 1.097k (±11.8%) i/s - 5.400k in 4.995296s
  69. calculate 1.091k (±12.0%) i/s - 5.370k in 4.998160s
  70.  
  71. truffleruby 0.30.2, like ruby 2.3.5 <Java HotSpot(TM) 64-Bit Server VM 1.8.0_151-b12 with Graal> [darwin-x86_64]
  72. Warming up --------------------------------------
  73. calculate 112.000 i/100ms
  74. calculate 157.000 i/100ms
  75. calculate 188.000 i/100ms
  76. Calculating -------------------------------------
  77. calculate 352.284k (±15.6%) i/s - 1.646M in 4.979090s
  78. calculate 352.140k (±15.0%) i/s - 1.707M in 4.987553s
  79. calculate 360.077k (±13.6%) i/s - 1.755M in 4.988948s
  80. ```
  81.  
  82. Running that it looks like MJIT is over 3x faster! Which is very impressive and it's already doing better than both JRuby and Rubinius.
  83.  
  84. TruffleRuby is over 300x faster (I only mention it because it's my own implementation of a Ruby JIT), so there's still lots of rooms for optimisations, as the authors have already said themselves.
Add Comment
Please, Sign In to add comment