3 ms·
"When simple strings with no variables in them are used, the performance is clearly better with double-quoted strings due to implementation details in the engin
by randallsquared 17y ago
"When simple strings with no variables in them are used, the performance is clearly better with double-quoted strings due to implementation details in the engine."
Is there any way this could be true except that they went out of their way to slow down single-quoted strings? How could it possibly be slower to do fewer steps?
- chaosmachine 17y agoIf I was going to guess, I'd say that double quoted strings are passed to a different "engine" than the single quotes, and perhaps over time the double quote engine has received better optimization.
- statictype 17y agoYeah, thats what I also figured but, really, how much can you optimize a constant string literal (which I'm given to understand the single-quote thing is)? Must be some kind of caching optimization and not really something that saves CPU cycles
- WilliamLP 17y agoSingle quotes aren't a constant string literal, because they have to parse \'.
- DougBTX 17y agoGuesstimate: if someone cares about performance, they should have already installed an opcode cache, which probably wouldn't have to re-parse constant strings. But then thinking: you'd expect constant double quoted strings to also get this treatment, so single quoting still gets you nothing. All the above is void without benchmarks.
- WilliamLP 17y agoI've found that the only way to get accurate information about performance is to test it myself or use a source that actually gives data. I tested this myself a while ago and found no detectable difference between single and double quotes. (And if I hadn't done this, I'd know not to trust this post:) )
- tremendo 17y agoThat does sound counter intuitive. But even if performance differences are negligible, I still find it much more efficient, or at least ergonomically comfortable, to use single quotes with only one finger/keystroke, no need for shift. Same reason I dislike PHP's arrow -> for methods/properties, that's three keys right there, vs. just a dot in other languages.
- mechanical_fish 17y agoThat quiet sound you sometimes hear is emacs, calling you. I can type the => character in one keystroke using emacs, though my key combo does technically contain two keys. I could reduce it to one if I really wanted to. (And I might, because I just found an entire redundant key on my Kinesis keyboard that I never use -- the one right below the X key. Thank you for prompting me to look for such a key. To an emacs user, finding an entire spare key under a finger is like striking gold.) Similarly, I found that Ruby doesn't use ; as often as :, so I swapped the two when in Ruby mode. That said, I don't like the arrow operator either, but it has nothing to do with typing. It has to do with the other side of ergonomics: Visual clutter when reading. In a similar vein, the array() operator is my pet peeve: tons of redundant characters, even if you invent a macro for typing it.
- encoderer 17y agoThe wording wasn't clear, but the example code given was: Using an interpolated double-quoted string is faster than using 2 or more concatenated single-quoted strings. That doesn't no longer holds true when comparing a non-interpolated double-quoted string and the identical single-quoted string Tho as others have said, if you're interested enough in optimization to worry about what type of quote you're using, you should just use opcode caching (APC) which will optimizing string parsing along with everything else.