On 2006-07-27, at 13:40 , Caio Chassot wrote: > > On 2006-07-27, at 13:36 , Caio Chassot wrote: > >>> 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. >> >> Well, if you implement chop fully, you get very similar results: > > Ah, but rangeless indexing yields much much better results: Speaking of catching typos, I apparently went too happy with my de- ranging and implemented the wrong thing. Here are the actual results. Pretty much the same as with ranges: RubyMate r4106 running Ruby v1.8.4 (/usr/local/bin/ruby) >>> untitled Rehearsal ------------------------------------------------- Indexing 3.690000 7.910000 11.600000 ( 13.937017) Chop 3.480000 7.890000 11.370000 ( 13.911387) Indexing crlf 3.690000 7.980000 11.670000 ( 15.256540) Chop crlf 3.530000 8.040000 11.570000 ( 16.200714) --------------------------------------- total: 46.210000sec user system total real Indexing 3.700000 8.050000 11.750000 ( 14.579216) Chop 3.520000 8.100000 11.620000 ( 15.165561) Indexing crlf 3.730000 8.090000 11.820000 ( 15.573669) Chop crlf 3.520000 8.100000 11.620000 ( 15.706817) --- require 'benchmark' n = 20_000 s = "I am a big string " * 5_000 Benchmark.bmbm do |bench| bench.report("Indexing") { n.times do s[-2,2] == "\r\n" ? s[0, s.length - 2] : s[0, s.length - 1] end } bench.report("Chop") { n.times do s.chop end } s << "\r\n" bench.report("Indexing crlf") { n.times do s[-2,2] == "\r\n" ? s[0, s.length - 2] : s[0, s.length - 1] end } bench.report("Chop crlf") { n.times do s.chop end } end