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

前端 未结 14 707
挽巷
挽巷 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条回答
  •  旧时难觅i
    2020-11-27 11:10

    Single quotes can be very slightly faster than double quotes because the lexer doesn't have to check for #{} interpolation markers. Depending on implementation, etc. Note that this is a parse-time cost, not a run-time cost.

    That said, the actual question was whether using double quoted strings "decreases performance in any meaningful way", to which the answer is a decisive "no". The difference in performance is so incredibly small that it is completely insignificant compared to any real performance concerns. Don't waste your time.

    Actual interpolation is a different story, of course. 'foo' will be almost exactly 1 second faster than "#{sleep 1; nil}foo".

提交回复
热议问题