Quoting matz / ruby-lang.org, on Thu, Oct 19, 2006 at 11:22:18PM +0900: > In message "Re: Symbol < String in Ruby > 1.8" > on Thu, 19 Oct 2006 15:56:24 +0900, Sam Roberts <sroberts / uniserve.com> writes: > > |Maybe things have changed, I benchmarked comparisons between short > |strings and symbols in ruby 1.8 last year to see if I could speed up a > |library that the profiler said was spending most of its time in > |String#==, and it didn't seem to make a difference. > > Interesting. Comparison should be much faster. I am not sure how you > benchmarked. You may hit a pitfall I should have found and fixed. > Can I see you benchmarking code? Can you find it? Of course, code is below. Its in the context of profiling I did of an iCalendar parser. There is a lot of comparisons of short strings, case-insensitively. Running a sample input under ruby -rprofile indicated that ~40% of the time was spent doing string comparisons, if I remember correctly. Anyhow, I considered converting strings to uppercase symbols during parsing and always using symbol comparison after that, but the benchmarking didn't make that look an improvement. Results: % ruby bench.rb user system total real noop 0.690000 0.000000 0.690000 ( 0.684341) String#== 2.430000 0.000000 2.430000 ( 2.430193) String#casecmp 2.630000 0.000000 2.630000 ( 2.636862) Symbol#== 2.250000 0.000000 2.250000 ( 2.272185) String#to_sym 2.480000 0.000000 2.480000 ( 2.475956) String#up 4.480000 0.010000 4.490000 ( 4.486260) String#up#to_sym 5.700000 0.000000 5.700000 ( 5.703154) % ruby -v ruby 1.8.4 (2005-12-24) [powerpc-darwin7.9.0] Is the code below reasonable? % cat bench.rb require 'benchmark' require 'pp' str0 = "a" * 20 str1 = str0.upcase sym0 = str0.to_sym sym1 = str1.to_sym $N = 1000000 Benchmark.bm(20) do |x| x.report("noop") do $N.times do end end x.report("String#==") do $N.times do str0 == str1 end end x.report("String#casecmp") do $N.times do str0.casecmp(str1) end end x.report("Symbol#==") do $N.times do sym0 == sym1 end end x.report("String#to_sym") do $N.times do str1.to_sym end end x.report("String#up") do $N.times do str1.upcase end end x.report("String#up#to_sym") do $N.times do str1.upcase.to_sym end end end