Guest User

Untitled

a guest
Jun 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. def range_to_arr_1(range, step=15.seconds)
  2. range.step(step)
  3. end
  4.  
  5. def range_to_arr_2(range, step=15.seconds)
  6. [].tap do |a|
  7. date = range.begin
  8. stop = range.end
  9. while date < stop do
  10. a << date
  11. date += step
  12. end
  13. end
  14. end
  15.  
  16. r1 = Time.now...Time.now+15.minutes
  17. r2 = Time.zone.now...Time.zone.now+15.minutes
  18. n1 = 150
  19. n2 = 150
  20.  
  21. Benchmark.bm do |x|
  22. x.report { for i in 1..n1; range_to_arr_1(r1); 1; end }
  23. x.report { for i in 1..n1; range_to_arr_2(r1); 1; end }
  24. x.report { for i in 1..n2; range_to_arr_1(r2); 1; end }
  25. x.report { for i in 1..n2; range_to_arr_2(r2); 1; end }
  26. end
  27.  
  28. # user system total real
  29. # 0.280000 0.030000 0.310000 ( 0.308992)
  30. # 0.090000 0.000000 0.090000 ( 0.092182)
  31. # 10.500000 0.000000 10.500000 ( 10.505025)
  32. # 0.210000 0.000000 0.210000 ( 0.206929)
Add Comment
Please, Sign In to add comment