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

前端 未结 14 675
挽巷
挽巷 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 11:05

    Thought I'd add a comparison of 1.8.7 and 1.9.2. I ran them a few times. Variance was about +-0.01.

    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("assign interp") { n.times do; c = "a #{n} 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}
      x.report("concat interp") { n.times do; "a #{n} string " + "b #{n} string"; end}
    end
    

    ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]

    assign single  0.180000   0.000000   0.180000 (  0.187233)
    assign double  0.180000   0.000000   0.180000 (  0.187566)
    assign interp  0.880000   0.000000   0.880000 (  0.877584)
    concat single  0.550000   0.020000   0.570000 (  0.567285)
    concat double  0.570000   0.000000   0.570000 (  0.570644)
    concat interp  1.800000   0.010000   1.810000 (  1.816955)
    

    ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]

      user          system      total      real
    assign single  0.140000   0.000000   0.140000 (  0.144076)
    assign double  0.130000   0.000000   0.130000 (  0.142316)
    assign interp  0.650000   0.000000   0.650000 (  0.656088)
    concat single  0.370000   0.000000   0.370000 (  0.370663)
    concat double  0.370000   0.000000   0.370000 (  0.370076)
    concat interp  1.420000   0.000000   1.420000 (  1.412210)
    

提交回复
热议问题