Performance about "..." and '...'

Hi,
I do not like to check the source code to see what exactly happend.
Could you tell me, is there a performance different between string
"…" and ‘…’ for simple string no parameter substitute.

For example, I saw a lot of code like :

puts “This is a String.” # case (1)
which is exactely the same as
puts ‘This is a String.’ # case (2)

It seems to me most programmer always use "…"
no matter it has parameter subsitute or not.

I like to know for simple string, like example above,
the case #2 will “faster” then case #1 ?

( if yes, I will “set up” my habit, when a simple string
always use ‘…’ )

Thanks for your help.

Email55555 wrote:

Hi,
I do not like to check the source code to see what exactly happend.
Could you tell me, is there a performance different between string
"…" and ‘…’ for simple string no parameter substitute.

For example, I saw a lot of code like :

puts “This is a String.” # case (1)
which is exactely the same as
puts ‘This is a String.’ # case (2)

It seems to me most programmer always use "…"
no matter it has parameter subsitute or not.

I like to know for simple string, like example above,
the case #2 will “faster” then case #1 ?

( if yes, I will “set up” my habit, when a simple string
always use ‘…’ )

Thanks for your help.

Try putting it in a loop and see what happens. For me:

g0@spud:~$ time ruby -e “5000000.times {‘This is a String.’}”

real 0m8.467s
user 0m8.470s
sys 0m0.000s
g0@spud:~$ time ruby -e “5000000.times {‘This is a String.’}”

real 0m8.419s
user 0m8.410s
sys 0m0.010s
g0@spud:~$ time ruby -e “5000000.times {‘This is a String.’}”

real 0m8.391s
user 0m8.390s
sys 0m0.000s
g0@spud:~$ time ruby -e ‘5000000.times {“This is a String.”}’

real 0m8.445s
user 0m8.430s
sys 0m0.010s
g0@spud:~$ time ruby -e ‘5000000.times {“This is a String.”}’

real 0m8.424s
user 0m8.420s
sys 0m0.010s
g0@spud:~$ time ruby -e ‘5000000.times {“This is a String.”}’

real 0m8.445s
user 0m8.440s
sys 0m0.000s

which suggests there isn’t really any practical difference (if my tests are
valid). Actually, unless there are cases where ‘…’ is noticably quicker,
perhaps it’d be more convenient to just make ‘…’ and “…” identical (a
la python)?

George Ogata wrote:

which suggests there isn’t really any practical difference (if my tests are
valid). Actually, unless there are cases where ‘…’ is noticably quicker,
perhaps it’d be more convenient to just make ‘…’ and “…” identical (a
la python)?

But they aren’t identical.
"…" allows alot of string interpolation, such as #{variables} and a
ton of backslash sequences.
’…’ is alot more restrictive, and only has a few backslash sequences.
(And is therefore faster for the interpreter to interpret.)

IMHO, if there is more than one way to do something, there should be a
reason. Now, your loop isn’t very helpful since it doesn’t DO all that
much. (The “…” vs ‘…’ difference only occurs once, at
interpretation.) However, once you have alot of code, a lot of string
constants, any string who doesn’t need to be checked for interpolation,
should use the ‘…’. It also makes it easier to write these strings,
since you don’t have to take into account a zillion things that need to
be escaped.

I tried two other tests:
500000.times{ eval “‘hi’” } # str1.rb
500000.times{ eval ‘“hi”’ } # str2.rb

With ruby 1.7.2 (2002-07-13) [i686-linux], there hardly was a difference

$ time ./str1.rb; time ./str2.rb
6.57user 0.05system 0:06.62elapsed 99%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (233major+2845minor)pagefaults 0swaps
6.55user 0.05system 0:06.59elapsed 100%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (233major+2845minor)pagefaults 0swaps

but with ruby 1.6.6 (2001-12-26) [i586-linux-gnu]

$ time ./str1.rb; time ./str2.rb
3.59user 0.01system 0:03.60elapsed 100%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (236major+260minor)pagefaults 0swaps
4.84user 0.03system 0:04.88elapsed 99%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (237major+2099minor)pagefaults 0swaps

Ugh, was 1.7.2 slower or does it just look like it? Anyway, I’m sticking
with my original intention, which is ‘…’ whenever possible, and "…"
when you need to be able to shoot yourself in the foot :slight_smile:

···


([ Kent Dahl ]/)_ ~ [ http://www.stud.ntnu.no/~kentda/ ]/~
))_student
/(( _d L b_/ NTNU - graduate engineering - 5. year )
( __õ|õ// ) )Industrial economics and technological management(
_
/ö____/ (_engineering.discipline=Computer::Technology)

I also used this style to distinguish between ‘constant’ strings and
those with x = ‘ing’; “vary#{x}” contents. But as soon as special
characters like newlines are included, you are forced to use double
quotes :frowning:

I’d prefer using single quotes for constant strings that can be
evaluated at compile time and double quotes for others.

Regards,
Pit

···

On 7 Aug 2002, at 19:43, Kent Dahl wrote:

(…)
Anyway, I’m
sticking with my original intention, which is ‘…’ whenever possible,
and “…” when you need to be able to shoot yourself in the foot :slight_smile:

Kent Dahl wrote:

George Ogata wrote:

which suggests there isn’t really any practical difference (if my tests
are

valid). Actually, unless there are cases where ‘…’ is noticably
quicker,

perhaps it’d be more convenient to just make ‘…’ and “…” identical (a
la python)?

But they aren’t identical.
"…" allows alot of string interpolation, such as #{variables} and a
ton of backslash sequences.
’…’ is alot more restrictive, and only has a few backslash sequences.
(And is therefore faster for the interpreter to interpret.)

I know they’re not identical, hence I was suggesting making both mean
"…". It wouldn’t be backward compatible, though, so I’m not expecting it.

IMHO, if there is more than one way to do something, there should be a
reason. Now, your loop isn’t very helpful since it doesn’t DO all that
much. (The “…” vs ‘…’ difference only occurs once, at
interpretation.)

Aha. Critical.

However, once you have alot of code, a lot of string
constants, any string who doesn’t need to be checked for interpolation,
should use the ‘…’. It also makes it easier to write these strings,
since you don’t have to take into account a zillion things that need to
be escaped.

Zillion? Only backslashes and #{…} constructs, no? Neither strike me as
being common (except backslashes in the Windows world). There’s still the
%{} construct too.

I tried two other tests:
500000.times{ eval “‘hi’” } # str1.rb
500000.times{ eval ‘“hi”’ } # str2.rb

With ruby 1.7.2 (2002-07-13) [i686-linux], there hardly was a difference

$ time ./str1.rb; time ./str2.rb
6.57user 0.05system 0:06.62elapsed 99%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (233major+2845minor)pagefaults 0swaps
6.55user 0.05system 0:06.59elapsed 100%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (233major+2845minor)pagefaults 0swaps

but with ruby 1.6.6 (2001-12-26) [i586-linux-gnu]

$ time ./str1.rb; time ./str2.rb
3.59user 0.01system 0:03.60elapsed 100%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (236major+260minor)pagefaults 0swaps
4.84user 0.03system 0:04.88elapsed 99%CPU (0avgtext+0avgdata
0maxresident)k
0inputs+0outputs (237major+2099minor)pagefaults 0swaps

Ugh, was 1.7.2 slower or does it just look like it? Anyway, I’m sticking
with my original intention, which is ‘…’ whenever possible, and "…"
when you need to be able to shoot yourself in the foot :slight_smile:

I find I shoot myself in the foot with ‘…’ more than “…”. I might have
a boring string, and so use ‘…’, but then add a #{…} or \n or something
in there, and forget to change the quotes. Maybe the rule is: “…” when
there’s any conceivable reason you might want to escape something in the
future, and ‘…’ for short, simple strings that you’ll never touch again.
Problem is putting in all that effort to hold down the shift key…

1.7’s slower for me too. And the difference between 1.6.7 and 1.7.2 is
much bigger than between using ‘…’ and “…”.