Last active
August 29, 2015 14:18
-
-
Save mfebrianto/ecc5c0770d41f3ea4cd4 to your computer and use it in GitHub Desktop.
performance comparison between grep vs select to get result of array_of_hash
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 'benchmark' | |
phone1 = { phone_number: '0434889994', plan: 'standard' } | |
phone2 = { phone_number: '0452649226', plan: 'standard' } | |
phone3 = { phone_number: '0434556677', plan: 'data_only' } | |
phone4 = { phone_numner: '0434889995', plan: 'data_only' } | |
array_of_hash = [phone1, phone2, phone3, phone4] | |
proc_find = ->(s) {s[:plan] == 'standard'} | |
puts "result of grep : #{ array_of_hash.grep(proc_find) }" | |
puts "result of select : #{ array_of_hash.select{|s| s[:plan] == 'standard'} }" | |
Benchmark.bm do |x| | |
x.report { 1000000.times { array_of_hash.grep(proc_find) } } | |
x.report { 1000000.times { array_of_hash.select{|s| s[:plan] == 'standard'} } } | |
end | |
#result of grep : [{:phone_number=>"0434889994", :plan=>"standard"}, {:phone_number=>"0452649226", :plan=>"standard"}] | |
#result of select : [{:phone_number=>"0434889994", :plan=>"standard"}, {:phone_number=>"0452649226", :plan=>"standard"}] | |
# user system total real | |
# 1.200000 0.000000 1.200000 ( 1.199973) | |
# 0.810000 0.000000 0.810000 ( 0.814260) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment