Advertisement
Guest User

Untitled

a guest
Oct 8th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. require "benchmark/ips"
  2.  
  3. Benchmark.ips do |x|
  4. x.config(time: 5, warmup: 2)
  5.  
  6. index = 42
  7. page_size = 10
  8.  
  9. x.report("separate") do |times|
  10. 1.upto(times) do
  11. page = index / page_size
  12. offset = index % page_size
  13. end
  14. end
  15.  
  16. x.report("separate i-assign") do |times|
  17. 1.upto(times) do
  18. page, offset = index / page_size, index % page_size
  19. end
  20. end
  21.  
  22. x.report("divmod") do |times|
  23. 1.upto(times) do
  24. page, offset = index.divmod(page_size)
  25. end
  26. end
  27.  
  28. x.report("divmod no i-assign") do |times|
  29. 1.upto(times) do
  30. page_and_offset = index.divmod(page_size)
  31. end
  32. end
  33.  
  34. x.report("separate no-assign") do |times|
  35. 1.upto(times) do
  36. index / page_size
  37. index % page_size
  38. end
  39. end
  40.  
  41. x.report("divmod no-assign") do |times|
  42. 1.upto(times) do
  43. index.divmod(page_size)
  44. end
  45. end
  46.  
  47. x.compare!
  48. end
  49.  
  50. # >> Calculating -------------------------------------
  51. # >> separate 109.038k i/100ms
  52. # >> separate i-assign 102.224k i/100ms
  53. # >> divmod 101.067k i/100ms
  54. # >> divmod no i-assign 103.093k i/100ms
  55. # >> separate no-assign 112.813k i/100ms
  56. # >> divmod no-assign 105.110k i/100ms
  57. # >> -------------------------------------------------
  58. # >> separate 12.945M (± 5.8%) i/s - 64.550M
  59. # >> separate i-assign 8.062M (± 2.9%) i/s - 40.276M
  60. # >> divmod 8.049M (± 2.5%) i/s - 40.225M
  61. # >> divmod no i-assign 8.001M (± 1.1%) i/s - 40.000M
  62. # >> separate no-assign 14.208M (± 3.0%) i/s - 70.959M
  63. # >> divmod no-assign 9.148M (± 1.1%) i/s - 45.828M
  64. # >>
  65. # >> Comparison:
  66. # >> separate no-assign: 14207716.5 i/s
  67. # >> separate: 12944759.7 i/s - 1.10x slower
  68. # >> divmod no-assign: 9148332.3 i/s - 1.55x slower
  69. # >> separate i-assign: 8062392.9 i/s - 1.76x slower
  70. # >> divmod: 8048842.0 i/s - 1.77x slower
  71. # >> divmod no i-assign: 8001018.8 i/s - 1.78x slower
  72. # >>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement