Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. require 'benchmark'
  2.  
  3. cases = [
  4. 'Word',
  5. 'Simple sentance',
  6. 'A one letter contatining sentance',
  7. 'Cover the punctuation!',
  8. '',
  9. 'Digits h3r3 and 4there'
  10. ]
  11.  
  12. def my_reverse(string)
  13. new_a = []
  14. t = string.size
  15. i = -1
  16. t.times do |time|
  17. new_a << string[i]
  18. i -= 1
  19. end
  20. new_a.join
  21. end
  22.  
  23. def native_reverse(string)
  24. string.reverse
  25. end
  26.  
  27.  
  28. def test_my_realisation(cases)
  29. cases.each do |string|
  30. result = my_reverse(string)
  31. test_results(string, result)
  32. end
  33. end
  34.  
  35. def test_built_in_realisation(cases)
  36. cases.each do |string|
  37. result = native_reverse(string)
  38. test_results(string, result)
  39. end
  40. end
  41.  
  42. def test_results(input,output)
  43. raise if input.reverse != output
  44. end
  45.  
  46. n = 5000
  47. Benchmark.bm do |x|
  48. x.report('my:') { n.times{ test_my_realisation(cases) } }
  49. x.report('ru:') { n.times{ test_built_in_realisation(cases) } }
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement