From: Robert Dober Date: 2007-06-22T01:57:35+09:00 Subject: Re: Code Critique: check if int or float On 6/21/07, ole __ wrote: > Yes but /^\d+/ isn't the same as /^-?\d+$/. > > -- > Posted via http://www.ruby-forum.com/. > > oops, well spotted, but that will not change a lot... 640/140 > cat conversions.rb && ruby conversions.rb # vim: sts=2 sw=2 tw=0 expandtab nu: require 'benchmark' class String def int? /^-?\d+$/ === self end def int1? Integer(self) rescue false end end N=10_000 Benchmark.bmbm do |bench| bench.report( "reg" ) { N.times{ |n| n.to_s.int?; "#{n}x".int? } } bench.report( "Int" ) { N.times{ |n| n.to_s.int1?; "#{n}x".int1? } } end Rehearsal --------------------------------------- reg 0.160000 0.000000 0.160000 ( 0.188396) Int 0.440000 0.000000 0.440000 ( 0.467686) ------------------------------ total: 0.600000sec user system total real reg 0.160000 0.000000 0.160000 ( 0.193306) Int 0.430000 0.010000 0.440000 ( 0.465137) -- You see things; and you say Why? But I dream things that never were; and I say Why not? -- George Bernard Shaw