When defining literal strings I tend to use single quotes instead of
double quotes. The only time I use double quotes is when I need my
string to be scanned for variable substitution or special characters.
The reasoning behind this is that I read somewhere that the Ruby
interpreter handles single quotes faster than double quotes because
it doesn’t have to parse the string looking for special characters.
Still I come across lots of code which never uses single quotes and
am guessing that the speed gain is not even noticeable in most cases.
Is my nitpicking going to help me out in the long run or am I just
waisting my time here. This minor issue has been itching my brain
for a while now.
At Thu, 23 Jan 2003 03:40:22 +0900, E F van de Laar wrote:
The reasoning behind this is that I read somewhere that the Ruby
interpreter handles single quotes faster than double quotes because
it doesn’t have to parse the string looking for special characters.
Still I come across lots of code which never uses single quotes and
am guessing that the speed gain is not even noticeable in most cases.
There are some special characters even in single quoted
literals. No significant difference unless you use millions of
\ and #.
Wednesday, January 22, 2003, 9:40:22 PM, you wrote:
The reasoning behind this is that I read somewhere that the Ruby
interpreter handles single quotes faster than double quotes because
it doesn’t have to parse the string looking for special characters.
Still I come across lots of code which never uses single quotes and
am guessing that the speed gain is not even noticeable in most cases.
20,000,000 characters have to be scanned to do the eval, and the difference
is miniscule. I ran this test ten times, and the single was always faster, but
it’s such a negligible difference that it doesn’t matter.
I hope you din’t mind me nitpicking. In each of those programs, you
only define a single string. You are not measuring the time to
construct a string with single-vs-double quotes, you are measuring the
time to print 100000 already-constructed strings!
Perhaps the 0.1 second time difference is explained by the fact that
the double-quoted version needs to look for more special characters.
But I doubt it.
Gavin
···
On Thursday, January 23, 2003, 6:53:11 AM, Daniel wrote:
On Thu, Jan 23, 2003 at 03:40:22AM +0900, E F van de Laar wrote:
Is my nitpicking going to help me out in the long run or am I just
waisting my time here. This minor issue has been itching my brain
for a while now.
Performance difference between single and double quotes does
exist only at compile time. Those two ASTs are absolutely
equivalent. Nothing differs while execution.
If you really want to measure the difference, you have to feed
very huge literals, or eval them.