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

前端 未结 14 669
挽巷
挽巷 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:01

    I modded Tim Snowhite's answer.

    require 'benchmark'
    n = 1000000
    attr_accessor = :a_str_single, :b_str_single, :a_str_double, :b_str_double
    @a_str_single = 'a string'
    @b_str_single = 'b string'
    @a_str_double = "a string"
    @b_str_double = "b string"
    @did_print = false
    def reset!
        @a_str_single = 'a string'
        @b_str_single = 'b string'
        @a_str_double = "a string"
        @b_str_double = "b string"
    end
    Benchmark.bm do |x|
        x.report('assign single       ') { n.times do; c = 'a string'; end}
        x.report('assign via << single') { c =''; n.times do; c << 'a string'; end}
        x.report('assign double       ') { n.times do; c = "a string"; end}
        x.report('assing interp       ') { n.times do; c = "a string #{'b 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 single interp') { n.times do; "#{@a_str_single}#{@b_str_single}"; end}
        x.report('concat single <<    ') { n.times do; @a_str_single << @b_str_single; end}
        reset!
        # unless @did_print
        #   @did_print = true
        #   puts @a_str_single.length 
        #   puts " a_str_single: #{@a_str_single} , b_str_single: #{@b_str_single} !!"
        # end
        x.report('concat double interp') { n.times do; "#{@a_str_double}#{@b_str_double}"; end}
        x.report('concat double <<    ') { n.times do; @a_str_double << @b_str_double; end}
    end
    

    Results:

    jruby 1.7.4 (1.9.3p392) 2013-05-16 2390d3b on Java HotSpot(TM) 64-Bit Server VM 1.7.0_10-b18 [darwin-x86_64]
           user     system      total        real
    assign single         0.220000   0.010000   0.230000 (  0.108000)
    assign via << single  0.280000   0.010000   0.290000 (  0.138000)
    assign double         0.050000   0.000000   0.050000 (  0.047000)
    assing interp         0.100000   0.010000   0.110000 (  0.056000)
    concat single         0.230000   0.010000   0.240000 (  0.159000)
    concat double         0.150000   0.010000   0.160000 (  0.101000)
    concat single interp  0.170000   0.000000   0.170000 (  0.121000)
    concat single <<      0.100000   0.000000   0.100000 (  0.076000)
    concat double interp  0.160000   0.000000   0.160000 (  0.108000)
    concat double <<      0.100000   0.000000   0.100000 (  0.074000)
    
    ruby 1.9.3p429 (2013-05-15 revision 40747) [x86_64-darwin12.4.0]
           user     system      total        real
    assign single         0.100000   0.000000   0.100000 (  0.103326)
    assign via << single  0.160000   0.000000   0.160000 (  0.163442)
    assign double         0.100000   0.000000   0.100000 (  0.102212)
    assing interp         0.110000   0.000000   0.110000 (  0.104671)
    concat single         0.240000   0.000000   0.240000 (  0.242592)
    concat double         0.250000   0.000000   0.250000 (  0.244666)
    concat single interp  0.180000   0.000000   0.180000 (  0.182263)
    concat single <<      0.120000   0.000000   0.120000 (  0.126582)
    concat double interp  0.180000   0.000000   0.180000 (  0.181035)
    concat double <<      0.130000   0.010000   0.140000 (  0.128731)
    

提交回复
热议问题