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

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

    There is no significant difference in either direction. It would have to be huge for it to matter.

    Except for times when you are sure that there is an actual problem with timing, optimize for programmer maintainability.

    The costs of machine time are very very small. The costs of programmer time to write code and maintain it is huge.

    What good is an optimization to save seconds, even minutes of runtime over thousands of runs if it means that the code is harder to maintain?

    Pick with a style and stick with it but do not pick that style based on statistically insignificant milliseconds of runtime.

    0 讨论(0)
  • 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".

    0 讨论(0)
提交回复
热议问题