Created
September 10, 2019 14:31
-
-
Save danielalvarenga/d9f8dea077c1d0977f3420b93a5c111d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'benchmark' | |
MAX = 100000 | |
results = {} | |
def if_return(attempt) | |
if attempt == 'primeira' | |
return 'primeira' | |
end | |
if attempt == 'segunda' | |
return 'segunda' | |
end | |
'terceira' | |
end | |
def if_elsif(attempt) | |
if attempt == 'primeira' | |
return 'primeira' | |
elsif attempt == 'segunda' | |
return 'segunda' | |
else | |
'terceira' | |
end | |
end | |
def switch_case(attempt) | |
case attempt | |
when 'primeira' | |
'primeira' | |
when 'segunda' | |
'segunda' | |
else | |
'terceira' | |
end | |
end | |
results[:if_return] = Benchmark.measure do | |
MAX.times do | |
if_return('tentativa') | |
end | |
end | |
results[:if_elsif] = Benchmark.measure do | |
MAX.times do | |
if_elsif('tentativa') | |
end | |
end | |
results[:switch_case] = Benchmark.measure do | |
MAX.times do | |
switch_case('tentativa') | |
end | |
end | |
results.each do |k, v| | |
puts "#{k}: #{v}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment