Skip to content

Instantly share code, notes, and snippets.

@baweaver
Created January 3, 2014 05:04
Show Gist options
  • Save baweaver/8233049 to your computer and use it in GitHub Desktop.
Save baweaver/8233049 to your computer and use it in GitHub Desktop.
String vs Symbol instantiation. When possible, prefer symbols, as they have on average half the instantiation time.
[9] pry(main)> n = 1_000
=> 1000
[10] pry(main)> Benchmark.bm { |bm|
[10] pry(main)* bm.report { n.times { :foo }}
[10] pry(main)* bm.report { n.times { 'foo'}}
[10] pry(main)* }
user system total real
0.000000 0.000000 0.000000 ( 0.000192)
0.000000 0.000000 0.000000 ( 0.000396)
=> [#<Benchmark::Tms:0x0000000263dfd8
@cstime=0.0,
@cutime=0.0,
@label="",
@real=0.000192456,
@stime=0.0,
@total=0.0,
@utime=0.0>,
#<Benchmark::Tms:0x000000026a6880
@cstime=0.0,
@cutime=0.0,
@label="",
@real=0.000395832,
@stime=0.0,
@total=0.0,
@utime=0.0>]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment