Skip to content

Instantly share code, notes, and snippets.

@metaskills
Created December 16, 2010 14:44
Show Gist options
  • Save metaskills/743466 to your computer and use it in GitHub Desktop.
Save metaskills/743466 to your computer and use it in GitHub Desktop.
which_is_easier_to_read.rb
%w(id foo under_score bar batz_score)
["id", "foo", "under_score", "bar", "batz_score"]
[:id, :foo, :under_score, :bar, :batz_score]
Author: Ken Collins
Date: December 16, 2010
Summary: Arrays
System Information
------------------
Operating System: Mac OS X 10.6.5 (10H574)
CPU: Quad-Core Intel Xeon 2.66 GHz
Processor Count: 4
Memory: 8 GB
ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10.4.0]
"literal w/symbols" is up to 28% faster over 1,000 repetitions
--------------------------------------------------------------
literal w/symbols 0.00223708152770996 secs Fastest
literal w/strings 0.00305581092834473 secs 26% Slower
%w() 0.00312900543212891 secs 28% Slower
require 'rubygems'
require 'bench_press'
extend BenchPress
author 'Ken Collins'
summary 'Arrays'
reps 1_000
measure "%w()" do
%w(id foo under_score bar batz_score).each { |attrib| }
end
measure "literal w/strings" do
["id", "foo", "under_score", "bar", "batz_score"].each { |attrib| }
end
measure "literal w/symbols" do
[:id, :foo, :under_score, :bar, :batz_score].each { |attrib| }
end
@kleb
Copy link

kleb commented Dec 16, 2010

I seem to recall a rant by Ara Howard about use of symbols being evil.

@josephholsten
Copy link

String#to_sym for arbitrary strings is evil because it adds to the symbol table, so it can cause your process to grow as it runs. If you're typing %w(foo bar), than these aren't arbitrary strings, they're important to your code. So you'll probably be using them again. Thus, this is probably not an evil use case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment