Created
September 8, 2012 04:10
-
-
Save jmdeldin/3671729 to your computer and use it in GitHub Desktop.
Split vs. count
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
require 'benchmark' | |
N = 10_000_000 | |
S = 'string,' * N | |
Benchmark.bmbm do |x| | |
x.report('#count') do | |
(length = S.count(',')) && (length + 1).times{|i| "line #{i} of #{length}" } | |
end | |
x.report('#split') do | |
split_s = S.split(',') | |
len = split_s.length | |
split_s.each_with_index{ |element, index| "line #{index} of #{len}"} | |
end | |
end |
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
% ruby bench.rb | |
Rehearsal ------------------------------------------ | |
#count 9.580000 0.000000 9.580000 ( 9.582891) | |
#split 13.090000 0.520000 13.610000 ( 13.614202) | |
-------------------------------- total: 23.190000sec | |
user system total real | |
#count 9.050000 0.260000 9.310000 ( 9.313300) | |
#split 11.690000 0.310000 12.000000 ( 11.999484) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With
N = 100
: