M
Mat Schaffer
I just did a quick benchmark to prove something to myself. But I'd
like to get a sanity check from the people on the list.
Basically I want to drop what will be a trailing "\n" from input.
But it appears that using String#[] and if statements is nearly 200
times more efficient than chop. Which just seems really weird, so
here's the benchmark. Maybe I'm doing something wrong.
Does this seem right? Anyone care to comment?
---- index_vs_chop.rb
require 'benchmark'
n = 100_000
bigstring = "I am a big string " * 5_000
Benchmark.bmbm do |bench|
bench.report("Indexing") {
n.times do
bigstring[0..-1]
end
}
bench.report("Chop") {
n.times do
bigstring.chop
end
}
end
---- end index_vs_shop.rb
output:
Rehearsal --------------------------------------------
Indexing 0.100000 0.000000 0.100000 ( 0.102362)
Chop 7.190000 13.890000 21.080000 ( 22.477807)
---------------------------------- total: 21.180000sec
user system total real
Indexing 0.100000 0.000000 0.100000 ( 0.108777)
Chop 7.290000 14.050000 21.340000 ( 22.755782)
like to get a sanity check from the people on the list.
Basically I want to drop what will be a trailing "\n" from input.
But it appears that using String#[] and if statements is nearly 200
times more efficient than chop. Which just seems really weird, so
here's the benchmark. Maybe I'm doing something wrong.
Does this seem right? Anyone care to comment?
---- index_vs_chop.rb
require 'benchmark'
n = 100_000
bigstring = "I am a big string " * 5_000
Benchmark.bmbm do |bench|
bench.report("Indexing") {
n.times do
bigstring[0..-1]
end
}
bench.report("Chop") {
n.times do
bigstring.chop
end
}
end
---- end index_vs_shop.rb
output:
Rehearsal --------------------------------------------
Indexing 0.100000 0.000000 0.100000 ( 0.102362)
Chop 7.190000 13.890000 21.080000 ( 22.477807)
---------------------------------- total: 21.180000sec
user system total real
Indexing 0.100000 0.000000 0.100000 ( 0.108777)
Chop 7.290000 14.050000 21.340000 ( 22.755782)