Is there a performance gain in using single quotes vs double quotes in ruby?

前端 未结 14 701
挽巷
挽巷 2020-11-27 10:32

Do you know if using double quotes instead of single quotes in ruby decreases performance in any meaningful way in ruby 1.8 and 1.9.

so if I type

qu         


        
14条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-27 10:53

    ~ > ruby -v   
    jruby 1.6.7 (ruby-1.8.7-p357) (2012-02-22 3e82bc8) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_37) [darwin-x86_64-java]
    ~ > cat qu.rb 
    require 'benchmark'
    
    n = 1000000
    Benchmark.bm do |x|
      x.report("assign single") { n.times do; c = 'a string'; end}
      x.report("assign double") { n.times do; c = "a string"; end}
      x.report("concat single") { n.times do; 'a string ' + 'b string'; end}
      x.report("concat double") { n.times do; "a string " + "b string"; end}
    end
    ~ > ruby qu.rb
          user     system      total        real
    assign single  0.186000   0.000000   0.186000 (  0.151000)
    assign double  0.062000   0.000000   0.062000 (  0.062000)
    concat single  0.156000   0.000000   0.156000 (  0.156000)
    concat double  0.124000   0.000000   0.124000 (  0.124000)
    

提交回复
热议问题