Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'benchmark'
- cases = [
- 'Word',
- 'Simple sentance',
- 'A one letter contatining sentance',
- 'Cover the punctuation!',
- '',
- 'Digits h3r3 and 4there'
- ]
- def my_reverse(string)
- new_a = []
- t = string.size
- i = -1
- t.times do |time|
- new_a << string[i]
- i -= 1
- end
- new_a.join
- end
- def native_reverse(string)
- string.reverse
- end
- def test_my_realisation(cases)
- cases.each do |string|
- result = my_reverse(string)
- test_results(string, result)
- end
- end
- def test_built_in_realisation(cases)
- cases.each do |string|
- result = native_reverse(string)
- test_results(string, result)
- end
- end
- def test_results(input,output)
- raise if input.reverse != output
- end
- n = 5000
- Benchmark.bm do |x|
- x.report('my:') { n.times{ test_my_realisation(cases) } }
- x.report('ru:') { n.times{ test_built_in_realisation(cases) } }
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement