From: Joel VanderWerf Date: 2009-07-21T04:24:56+09:00 Subject: Re: getting char from string as fixnum in 1.8 and 1.9 Joel VanderWerf wrote: > David A. Black wrote: >> def third_char_as_fixnum(s) >> s[2,1].unpack("C*")[0] >> end About a factor of three slower, as I measure it: require 'benchmark' if RUBY_VERSION =~ /\A1\.9/ def third_char_as_fixnum_1(s) s[2].ord end else def third_char_as_fixnum_1(s) s[2] end end def third_char_as_fixnum_2(s) s[2,1].unpack("C").first end s = "abc" N = 1_000_000 Benchmark.bmbm(12) do |bm| bm.report("1") {N.times {third_char_as_fixnum_1(s)}} bm.report("2") {N.times {third_char_as_fixnum_2(s)}} end __END__ Rehearsal ----------------------------------------------- 1 1.060000 0.000000 1.060000 ( 1.079800) 2 1.620000 0.000000 1.620000 ( 1.634525) -------------------------------------- total: 2.680000sec user system total real 1 0.580000 0.000000 0.580000 ( 0.598929) 2 1.640000 0.000000 1.640000 ( 1.664318) -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407