eggie5
9/25/2007 5:48:00 PM
On Sep 25, 1:31 am, "Jes?s Gabriel y Gal?n" <jgabrielyga...@gmail.com>
wrote:
> On 9/25/07, Pe?a, Botp <b...@delmonte-phil.com> wrote:
>
> > From: David A. Black [mailto:dbl...@rubypal.com]
>
> > #Don't forget \D:
> > #
> > # old_num.gsub(/\D/, "")
>
> I always wanted try the benchmark stuff, and this seemed like a good
> opportunity :-).
>
> cat remove_nondigits.rb && ruby remove_nondigits.rb
> # remove_nondigits.rb
> # 25 September 2007
> #
>
> require 'benchmark'
>
> n = 100_000
> old_num = "(555) 55-555-55"
>
> Benchmark.bmbm do |x|
> x.report("scan") do
> n.times {old_num.scan(/\d/).join}
> end
> x.report("gsub") do
> n.times {old_num.gsub(/[^\d]/,"")}
> end
> x.report("gsub2") do
> n.times {old_num.gsub(/\D/, "")}
> end
> x.report("tr") do
> n.times {old_num.tr("^0-9","")}
> end
> x.report("delete") do
> n.times {old_num.delete("^0-9")}
> end
> x.report("split") do
> n.times {old_num.split(/[^\d]/).join}
> end
> end
> Rehearsal ------------------------------------------
> scan 1.080000 0.000000 1.080000 ( 1.144654)
> gsub 0.380000 0.010000 0.390000 ( 0.403057)
> gsub2 0.390000 0.010000 0.400000 ( 0.416074)
> tr 0.290000 0.020000 0.310000 ( 0.320764)
> delete 0.270000 0.010000 0.280000 ( 0.294078)
> split 0.700000 0.000000 0.700000 ( 0.714139)
> --------------------------------- total: 3.160000sec
>
> user system total real
> scan 1.080000 0.020000 1.100000 ( 1.101550)
> gsub 0.380000 0.010000 0.390000 ( 0.404968)
> gsub2 0.380000 0.010000 0.390000 ( 0.409364)
> tr 0.290000 0.020000 0.310000 ( 0.318723)
> delete 0.280000 0.010000 0.290000 ( 0.294650)
> split 0.700000 0.000000 0.700000 ( 0.713261)
>
> So it seems that in terms of speed, delete wins, second place for tr,
> and in the fight between [^\d] and \D the first one wins by a little
> (very little) margin :-).
>
> Cheers,
>
> Jesus.
That's awesome, thanks.