I know this is a very simply question, but I've seen many different responses.
Isn't it better to use 'some string' instead of "some string". Because the "" goes through a lot more working interpreting any variable that might be in the string, etc. Where as '' is just literal.
Aesthetics aside, Ruby optomizes double quoted strings that don't have any
interpolation so the performance difference between the two is irrelevant.
marcel
···
On Fri, Jul 21, 2006 at 03:48:09AM +0900, Ben Johnson wrote:
I know this is a very simply question, but I've seen many different
responses.
Isn't it better to use 'some string' instead of "some string".
Because the "" goes through a lot more working interpreting any
variable that might be in the string, etc. Where as '' is just literal.
I know this is a very simply question, but I've seen many different responses.
Isn't it better to use 'some string' instead of "some string". Because the "" goes through a lot more working interpreting any variable that might be in the string, etc. Where as '' is just literal.
Thanks for your help.
Thank You,
Ben Johnson
E: bjohnson@contuitive.com
I like to think it makes a difference, but...
Don't know if this is a good benchmark or not (probably not):
Doesn't look like it. Guess Ruby is pretty smart about handling this.
End facts. Begin opinions...
I use to use '...' all the time thinking it was a good programming habit. Unfortunately, it just meant I had to switch to "..." every time I belatedly realized I would need some interpolation. So, as I've gotten lazier, I've pretty much switched to using "..." all the time.
James Edward Gray II
···
On Jul 20, 2006, at 1:48 PM, Ben Johnson wrote:
I know this is a very simply question, but I've seen many different responses.
Isn't it better to use 'some string' instead of "some string". Because the "" goes through a lot more working interpreting any variable that might be in the string, etc. Where as '' is just literal.
I usually use double quotes since my strings often have apostrophes in 'em.
'Tis better'n always escapin' 'em anyways. Maybe it's just me tho'.
---John
···
On 7/20/06, Ben Johnson <bjohnson@contuitive.com> wrote:
I know this is a very simply question, but I've seen many different
responses.
Isn't it better to use 'some string' instead of "some string".
Because the "" goes through a lot more working interpreting any
variable that might be in the string, etc. Where as '' is just literal.
I know this is a very simply question, but I've seen many different
responses.
Isn't it better to use 'some string' instead of "some string".
Because the "" goes through a lot more working interpreting any
variable that might be in the string, etc. Where as '' is just literal.
There is no difference at all after parsing, both get turned into a NODE_STR (unless there are interpolations):
%{"abc"}.parse_to_nodes.transform
=> [:str, {:lit=>"abc"}]
%{'abc'}.parse_to_nodes.transform
=> [:str, {:lit=>"abc"}]
And I don't think that there is any significant performance difference while parsing, so use whatever you like more.
Dominik
···
On Thu, 20 Jul 2006 20:48:09 +0200, Ben Johnson <bjohnson@contuitive.com> wrote:
If the interpreter sees a single-quoted string, it knows
that it doesn't need to scan the string for things (such
as escape sequences) to expand. I agree that this effect
will be lost in the noise, in most cases, but why make the
interpreter do extra work?
The real point, in any case, is one of making things clear
to the reader. If a quoted string uses double quotes, the
reader has to look it over to determine whether any magic
is being performed. This wastes some of the reader's time
(and no, I don't have a benchmark for this. :-).
-r
···
At 3:10 AM +0900 9/26/06, James Edward Gray II wrote:
At Tue, 26 Sep 2006 04:09:54 +0900,
Rich Morin wrote in [ruby-talk:216346]:
···
At 3:10 AM +0900 9/26/06, James Edward Gray II wrote:
>> Use '' by default:
>>
>> - It's less work for the interpreter.
>
> What makes you say this?
If the interpreter sees a single-quoted string, it knows
that it doesn't need to scan the string for things (such
as escape sequences) to expand. I agree that this effect
will be lost in the noise, in most cases, but why make the
interpreter do extra work?
It knows that it does need to scan the string always for the
terminator, and even escaped quotes.
The real point, in any case, is one of making things clear
to the reader. If a quoted string uses double quotes, the
reader has to look it over to determine whether any magic
is being performed. This wastes some of the reader's time
(and no, I don't have a benchmark for this. :-).
True, but syntax hiliting can help spot the #{...}.
···
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
user system total real
double: 32.160000 1.040000 33.200000 ( 56.097062)
single: 32.150000 1.090000 33.240000 ( 53.597692)
rick@frodo:/public/rubyscripts$
···
On 9/25/06, Christian Neukirchen <chneukirchen@gmail.com> wrote:
James Edward Gray II <james@grayproductions.net> writes:
> On Sep 25, 2006, at 12:03 PM, Rich Morin wrote:
>
>> Use '' by default:
>>
>> - It's less work for the interpreter.
>
> What makes you say this?
>
> #!/usr/bin/env ruby -w
>
> require "benchmark"
>
> TESTS = 1_000_000
> Benchmark.bmbm(10) do |results|
> results.report("double:") { TESTS.times { "James" } }
> results.report("single:") { TESTS.times { 'James' } }
> end
You really should compare the parse-trees of these before even trying
to benchmark it that way...
James Edward Gray II <james@grayproductions.net> writes:
>
>> Use '' by default:
>>
>> - It's less work for the interpreter.
>
> What makes you say this?
>
> #!/usr/bin/env ruby -w
>
> require "benchmark"
>
> TESTS = 1_000_000
> Benchmark.bmbm(10) do |results|
> results.report("double:") { TESTS.times { "James" } }
> results.report("single:") { TESTS.times { 'James' } }
> end
You really should compare the parse-trees of these before even trying
to benchmark it that way...
In the non-interpolation case the parse-trees are identical:
At 3:10 AM +0900 9/26/06, James Edward Gray II wrote:
On Sep 25, 2006, at 12:03 PM, Rich Morin wrote:
Use '' by default:
- It's less work for the interpreter.
What makes you say this?
If the interpreter sees a single-quoted string, it knows
that it doesn't need to scan the string for things (such
as escape sequences) to expand. I agree that this effect
will be lost in the noise, in most cases, but why make the
interpreter do extra work?
--
Eric Hodel - drbrain@segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant
user system total real
double: 32.160000 1.040000 33.200000 ( 56.097062)
single: 32.150000 1.090000 33.240000 ( 53.597692)
rick@frodo:/public/rubyscripts$
And now the difference gets lost in noise because the strings aren't
long enough.
I was speaking of the entire ruby executable as the
interpreter. Clearly, the parsing subsystem is the
only part (if any) that does more work. However, as
I pointed out, this isn't the real issue.
Indicating the possibility of "magic" behavior, without
the need for the reader to look inside the quotes, is
my principle concern.
-r
···
At 5:29 PM +0900 9/29/06, Eric Hodel wrote:
Incorrect. For identical string contents the interpreter
does the same amount of work. The parser may do less work,
but you only parse once.