Created
July 29, 2014 08:55
-
-
Save jodosha/8a6c3b8381a011b50302 to your computer and use it in GitHub Desktop.
Lambda style queries for Lotus::Model hanami/model#42
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
#!/usr/bin/env ruby | |
require 'benchmark' | |
TIMES = (ENV['TIMES'] || 10_000_000).to_i | |
class Query | |
def where_with_unless(condition = nil, &blk) | |
raise ArgumentError, 'You need to specify an condition.' unless condition or block_given? | |
end | |
def where_with_or_blk(condition = nil, &blk) | |
condition or blk or raise ArgumentError.new('You need to specify an condition.') | |
end | |
def where_with_or_block_given(condition = nil, &blk) | |
condition or block_given? or raise ArgumentError.new('You need to specify an condition.') | |
end | |
end | |
query = Query.new | |
hash_condition = { age: 32 } | |
lambda_condition = -> { age > 32 } | |
Benchmark.bm(40) do |bm| | |
bm.report 'where with unless (rescue)' do | |
TIMES.times do | |
query.where_with_unless rescue nil | |
end | |
end | |
bm.report 'where with or blk (rescue)' do | |
TIMES.times do | |
query.where_with_or_blk rescue nil | |
end | |
end | |
bm.report 'where with or block given (rescue)' do | |
TIMES.times do | |
query.where_with_or_block_given rescue nil | |
end | |
end | |
bm.report 'where with unless (hash)' do | |
TIMES.times do | |
query.where_with_unless(hash_condition) | |
end | |
end | |
bm.report 'where with or blk (hash)' do | |
TIMES.times do | |
query.where_with_or_blk(hash_condition) | |
end | |
end | |
bm.report 'where with or block given (hash)' do | |
TIMES.times do | |
query.where_with_or_block_given(hash_condition) | |
end | |
end | |
bm.report 'where with unless (lambda)' do | |
TIMES.times do | |
query.where_with_unless(lambda_condition) | |
end | |
end | |
bm.report 'where with or blk (lambda)' do | |
TIMES.times do | |
query.where_with_or_blk(lambda_condition) | |
end | |
end | |
bm.report 'where with or block given (lambda)' do | |
TIMES.times do | |
query.where_with_or_block_given(lambda_condition) | |
end | |
end | |
end | |
__END__ | |
Output: | |
user system total real | |
where with unless (rescue) 19.650000 0.680000 20.330000 ( 20.343976) | |
where with or blk (rescue) 20.180000 0.470000 20.650000 ( 20.652801) | |
where with or block given (rescue) 21.670000 0.610000 22.280000 ( 22.281081) | |
where with unless (hash) 1.120000 0.000000 1.120000 ( 1.118581) | |
where with or blk (hash) 1.070000 0.000000 1.070000 ( 1.067491) | |
where with or block given (hash) 1.190000 0.000000 1.190000 ( 1.190203) | |
where with unless (lambda) 1.170000 0.000000 1.170000 ( 1.174007) | |
where with or blk (lambda) 1.130000 0.000000 1.130000 ( 1.129684) | |
where with or block given (lambda) 1.110000 0.000000 1.110000 ( 1.111652) | |
Ruby: | |
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0] | |
Hardware: | |
Hardware Overview: | |
Model Name: MacBook Air | |
Model Identifier: MacBookAir5,2 | |
Processor Name: Intel Core i7 | |
Processor Speed: 2 GHz | |
Number of Processors: 1 | |
Total Number of Cores: 2 | |
L2 Cache (per Core): 256 KB | |
L3 Cache: 4 MB | |
Memory: 8 GB | |
Boot ROM Version: MBA51.00EF.B02 | |
SMC Version (system): 2.5f9 | |
Software: | |
System Software Overview: | |
System Version: OS X 10.9.4 (13E28) | |
Kernel Version: Darwin 13.3.0 | |
Time since boot: 2 days 12:05 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment