Created
December 16, 2010 14:44
-
-
Save metaskills/743466 to your computer and use it in GitHub Desktop.
which_is_easier_to_read.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%w(id foo under_score bar batz_score) | |
["id", "foo", "under_score", "bar", "batz_score"] | |
[:id, :foo, :under_score, :bar, :batz_score] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
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
I seem to recall a rant by Ara Howard about use of symbols being evil.