Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Created April 27, 2012 17:36
Show Gist options
  • Save tenderlove/2511099 to your computer and use it in GitHub Desktop.
Save tenderlove/2511099 to your computer and use it in GitHub Desktop.
require 'active_record'
require 'benchmark/ips'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => ':memory:')
ActiveRecord::Base.connection.create_table('posts') do |t|
t.string :name
t.timestamps
end
class Post < ActiveRecord::Base
end
post = Post.create(:name => 'aaron')
id = post.id
Benchmark.ips do |x|
x.report("new") { Post.new }
x.report("new args") { Post.new(:name => 'aaron') }
x.report("create") { Post.create(:name => 'aaron') }
x.report("find") { Post.find(id) }
end
__END__
3-2-stable:
Calculating -------------------------------------
new 1597 i/100ms
new args 755 i/100ms
create 91 i/100ms
find 193 i/100ms
-------------------------------------------------
new 24843.5 (±8.8%) i/s - 124566 in 5.056542s
new args 9188.9 (±9.5%) i/s - 45300 in 4.998662s
create 932.8 (±7.3%) i/s - 4641 in 5.002245s
find 2066.4 (±5.5%) i/s - 10422 in 5.058268s
master:
Calculating -------------------------------------
new 2104 i/100ms
new args 871 i/100ms
create 95 i/100ms
find 213 i/100ms
-------------------------------------------------
new 33856.6 (±7.9%) i/s - 168320 in 5.005236s
new args 10681.3 (±7.3%) i/s - 53131 in 5.002680s
create 970.3 (±7.0%) i/s - 4845 in 5.019001s
find 2207.7 (±7.2%) i/s - 11076 in 5.042719s
@deepfryed
Copy link

AR, Y U SO SLOW ?

#!/usr/bin/env ruby

require 'benchmark/ips'
require 'swift'
require 'swift/migrations'

Swift.setup(:default, Swift::DB::Sqlite3, db: ":memory:")

class Post < Swift::Scheme
  store :posts

  attribute :id,         Swift::Type::Integer, serial: true, key: true
  attribute :name,       Swift::Type::String
  attribute :created_at, Swift::Type::DateTime, default: proc { DateTime.now }
  attribute :updated_at, Swift::Type::DateTime, default: proc { DateTime.now }
end

Swift.migrate!

Benchmark.ips do |x|
  x.report("new")       { Post.new }
  x.report("new args")  { Post.new(name: 'aaron') }
  x.report("create")    { Post.create(name: 'aaron') }
end

__END__
Intel(R) Core(TM) i7-2677M CPU @ 1.80GHz

Calculating -------------------------------------
                 new     14167 i/100ms
            new args      9770 i/100ms
              create      2014 i/100ms
-------------------------------------------------
                 new   173336.2 (±2.1%) i/s -     878354 in   5.069734s
            new args   116411.6 (±1.2%) i/s -     586200 in   5.036291s
              create    20918.9 (±0.5%) i/s -     104728 in   5.006505s

@tenderlove
Copy link
Author

Perhaps because we're not comparing apples to apples. :-)

@jessedearing
Copy link

Looks great!

Thank you for the amazing work.

❤️

@skanev
Copy link

skanev commented Apr 28, 2012

❤️

@cyril
Copy link

cyril commented Apr 28, 2012

❤️

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