Skip to content

Instantly share code, notes, and snippets.

require 'benchmark/ips'
bar = 'bar'
Benchmark.ips do |r|
r.report('concatenation') { 'foo' << bar }
r.report('interpolation') { "foo#{bar}" }
end
@jonleighton
jonleighton / attr_reader.rb
Created October 10, 2012 21:27
Perf comparison of attr_reader vs methods on ruby 1.9.3
require 'benchmark/ips'
class Foo1
def initialize
@bar = :bar
end
attr_reader :bar
end
Calculating -------------------------------------
symbol 51632 i/100ms
string 46997 i/100ms
-------------------------------------------------
symbol 2537871.3 (±1.1%) i/s - 12701472 in 5.005420s
string 1664204.2 (±7.7%) i/s - 8271472 in 5.012292s
require 'active_record'
require 'benchmark/ips'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
class Post < ActiveRecord::Base
connection.create_table :posts do |t|
t.string :name
end
end
require 'benchmark/ips'
class A
def initialize
@attributes = { :foo => 1 }
end
def foo
@attributes[:foo]
end
$ ruby test.rb
Calculating -------------------------------------
success 51960 i/100ms
failure 4653 i/100ms
-------------------------------------------------
success 2687778.5 (±3.7%) i/s - 13405680 in 4.996239s
failure 53863.7 (±3.5%) i/s - 269874 in 5.016999s
@jonleighton
jonleighton / results.txt
Created September 14, 2012 14:57
A look at how ObjectSpace finalizers affect GC time
$ ruby test.rb false
0.210000 0.000000 0.210000 ( 0.212155)
$ ruby test.rb true
10.000000 0.280000 10.280000 ( 10.773232)
Generating data...
Inserting 20000 users and exhibits...
Calculating -------------------------------------
Model#id 9279 i/100ms
Model.new (instantiation)
2056 i/100ms
Model.new (setting attributes)
890 i/100ms
Model.first 178 i/100ms
Model.all limit(100) 5 i/100ms
require 'benchmark/ips'
h = { foo: :bar }
Benchmark.ips do |r|
r.report('#[]') { h[:foo] }
r.report('#fetch') { h.fetch(:foo) }
end
Operating system: Mac OS X
10.7.4 11E53
CPU: x86
GenuineIntel family 6 model 42 stepping 7
4 CPUs
Crash reason: EXC_BAD_ACCESS / KERN_INVALID_ADDRESS
Crash address: 0x6e6f4640
Thread 0 (crashed)