Skip to content

Instantly share code, notes, and snippets.

@tagomoris
Created November 25, 2016 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tagomoris/d9cf1e961378ab553b65fbaa844ba6df to your computer and use it in GitHub Desktop.
Save tagomoris/d9cf1e961378ab553b65fbaa844ba6df to your computer and use it in GitHub Desktop.
N=1_000
line = "a" * 200 + "\n"
body = line * 300
Benchmark.bm{|x|
x.report("splitonly") {
N.times{
buf = body.dup
lines = []
while !buf.empty?
lines << buf.slice!(0,201)
end
}
}
x.report("regexp") {
N.times{
buf = body.dup
lines = []
while line = buf.slice!(/.*?\n/m)
lines << line
end
}
}
x.report("index") {
N.times{
buf = body.dup
lines = []
while idx = buf.index("\n")
lines << buf.slice!(0, idx + 1)
end
}
}
}
$ ruby -rbenchmark hoge.rb
user system total real
splitonly 2.320000 1.280000 3.600000 ( 3.618215)
regexp 4.490000 1.240000 5.730000 ( 5.754846)
index 2.020000 0.690000 2.710000 ( 2.711234)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment