From: Jake McArthur Date: 2006-07-28T01:41:12+09:00 Subject: Re: String#chop slow? REALLY slow? On Jul 27, 2006, at 11:12 AM, Mat Schaffer wrote: > Basically I want to drop what will be a trailing "\n" from input. String#chomp would probably be a better idea for this, but that's OT I suppose. Regardless, its performance is the same as chop, it seems. Here are my modifications: require 'benchmark' class String def my_chop self[0..-2] end end 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 } bench.report("My Chop") { n.times do bigstring.my_chop end } end And here are my results: Rehearsal -------------------------------------------- Indexing 0.310000 0.000000 0.310000 ( 0.347943) Chop 11.940000 30.330000 42.270000 ( 44.501066) My Chop 12.620000 30.720000 43.340000 ( 46.339651) ---------------------------------- total: 85.920000sec user system total real Indexing 0.230000 0.000000 0.230000 ( 0.258177) Chop 11.980000 30.680000 42.660000 ( 44.966923) My Chop 12.610000 30.860000 43.470000 ( 45.859064) Let's see how String#chop is implemented... static VALUE rb_str_chop(str) VALUE str; { str = rb_str_dup(str); rb_str_chop_bang(str); return str; } So it's in C... interesting.... - Jake McArthur