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
···
–
([ 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)