Created
December 19, 2009 23:34
-
-
Save paul/260280 to your computer and use it in GitHub Desktop.
Performance of DataMapper & ActiveRecord .new, no database access
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
# 1.8.7 | |
| Ruby Class | Hash Model | Datapathy #new | Datapathy #new_from_attributes | DM 0.10.2 | AR 3.0.pre | | |
------------------------------------------------------------------------------------------------------------------------------------------- | |
#new (no attributes) x100000 | 0.562 | 0.348 | 0.534 | 0.669 | 0.478 | 10.931 | | |
#new (3 attributes) x100000 | 0.362 | 0.383 | 1.596 | 0.539 | 16.030 | 28.158 | | |
# 1.9.1 | |
| Ruby Class | Hash Model | Datapathy #new | Datapathy #new_from_attributes | DM 0.10.2 | AR 3.0.pre | | |
------------------------------------------------------------------------------------------------------------------------------------------- | |
#new (no attributes) x100000 | 0.281 | 0.216 | 0.231 | 0.302 | 0.294 | 10.477 | | |
#new (3 attributes) x100000 | 0.229 | 0.376 | 1.707 | 0.258 | 11.924 | 26.898 | | |
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 'rbench' | |
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../lib")) | |
require 'datapathy' | |
class DatapathyModel | |
include Datapathy::Model | |
persists :id, :title, :text | |
end | |
require 'dm-core' | |
class DataMapperModel | |
include DataMapper::Resource | |
property :id, Integer, :key => true | |
property :title, String | |
property :text, String | |
end | |
DataMapper.setup(:default, :adapter => :in_memory) | |
require 'active_record' | |
ActiveRecord::Base.establish_connection( | |
:adapter => "sqlite3", | |
:database => "benchmark.db" | |
) | |
ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS active_record_models") | |
ActiveRecord::Base.connection.execute("CREATE TABLE active_record_models (id INTEGER UNIQUE, title STRING, text STRING)") | |
class ActiveRecordModel < ActiveRecord::Base | |
end | |
# Have AR scan the table before the benchmark | |
ActiveRecordModel.new | |
class PlainModel | |
attr_accessor :id, :title, :text | |
def initialize(attrs = {}) | |
@id, @title, @text = attrs[:id], attrs[:title], attrs[:text] | |
end | |
end | |
class HashModel | |
def initialize(attributes = {}) | |
attrs = {} | |
attrs.merge!(attributes) | |
end | |
end | |
ATTRS = {:id => 1, :title => "Foo", :text => "Bar"} | |
RBench.run(100_000) do | |
column :times | |
column :plain, :title => "Ruby Class" | |
column :hash, :title => "Hash Model" | |
column :datapathy, :title => "Datapathy #new" | |
column :datapathy_a, :title => "Datapathy #new_from_attributes" | |
column :datamapper, :title => "DM #{DataMapper::VERSION}" | |
column :ar, :title => "AR 3.0.pre" | |
report "#new (no attributes)" do | |
plain do | |
PlainModel.new | |
end | |
hash do | |
HashModel.new | |
end | |
datapathy do | |
DatapathyModel.new | |
end | |
datapathy_a do | |
DatapathyModel.new_from_attributes | |
end | |
datamapper do | |
DataMapperModel.new | |
end | |
ar do | |
ActiveRecordModel.new | |
end | |
end | |
report "#new (3 attributes)" do | |
plain do | |
PlainModel.new ATTRS | |
end | |
hash do | |
HashModel.new ATTRS | |
end | |
datapathy do | |
DatapathyModel.new ATTRS | |
end | |
datapathy_a do | |
DatapathyModel.new_from_attributes ATTRS | |
end | |
datamapper do | |
DataMapperModel.new ATTRS | |
end | |
ar do | |
ActiveRecordModel.new ATTRS | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment