Skip to content

Instantly share code, notes, and snippets.

@msg7086
Created December 30, 2015 21:35
Show Gist options
  • Save msg7086/d1825a6f0f2dff322db1 to your computer and use it in GitHub Desktop.
Save msg7086/d1825a6f0f2dff322db1 to your computer and use it in GitHub Desktop.
/sinatra/sinatra/pull/1059
def a(unsafe)
chars = unsafe.split(//).map! do |char|
if char =~ /[A-Z]/
char <<= char.tr('A-Z', 'a-z')
end
char
end
end
def b(unsafe)
chars = unsafe.split(//).map! do |char|
char_dup = char.dup
char_dup << (char.downcase! || '')
end
end
def c(unsafe)
chars = unsafe.split(//).map do |ch|
ch == ch.downcase ? ch : ch + ch.downcase
end
end
require 'benchmark'
SRC = 'A8iojJIWQjdkwjuiop*U(@!8mjoid21JJKLWJQEDdwqi91pj'.freeze
n = 20000
Benchmark.bm(10) do |x|
x.report('sinatra') { n.times { a(SRC); } }
x.report('aidewoode') { n.times { b(SRC); } }
x.report('condition') { n.times { c(SRC); } }
end
require 'minitest/autorun'
class CodeTest < Minitest::Test
def test_result
assert_equal a(SRC), c(SRC)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment