Created
February 11, 2012 10:03
-
-
Save stim371/1798377 to your computer and use it in GitHub Desktop.
benchmarking different ways of splitting up a string
This file contains 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
sentence = "rake routes --help this last part" | |
def with_split sentence | |
a = sentence.split('--') | |
@build_args = "--" << a[1] | |
@args = a[0] | |
end | |
def with_slice sentence | |
dash_ind = sentence.index("--") | |
@build_args = sentence.slice(dash_ind + 1...sentence.length) | |
@args = sentence.slice(0...dash_ind) | |
end | |
def with_index sentence | |
dash_ind = sentence.index("--") | |
@build_args = sentence[dash_ind + 1...sentence.length] | |
@args = sentence[0...dash_ind] | |
end | |
def test_time(start_time, end_time) | |
end_time - start_time | |
end | |
%w[with_split with_slice with_index].each do |method_name| | |
start_time = Time.now | |
10000.times do | |
send method_name.to_sym, sentence | |
end | |
end_time = Time.now | |
puts "#{method_name} method works in #{test_time(start_time, end_time)} seconds" | |
puts "#{method_name}: build_args are: '#{@build_args}'" | |
puts "#{method_name}: args are: '#{@args}'\n\n" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment