Last active
December 17, 2015 17:09
-
-
Save johno/5644366 to your computer and use it in GitHub Desktop.
Test the performance differences between an array and hash for key lookup with the Swot gem. Run with `$ ruby performance_test.rb <number_of_tests || default: 100>`
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
module ArrayDataStruct | |
ACADEMIC_TLDS = [ | |
'ac.ae', | |
'ac.at', | |
'ac.bd', | |
'ac.be', | |
'ac.cr', | |
'ac.cy', | |
'ac.fj', | |
'ac.id', | |
'ac.il', | |
'ac.ir', | |
'ac.jp', | |
'ac.ke', | |
'ac.kr', | |
'ac.ma', | |
'ac.mu', | |
'ac.mw', | |
'ac.mz', | |
'ac.nz', | |
'ac.pa', | |
'ac.pg', | |
'ac.rs', | |
'ac.ru', | |
'ac.rw', | |
'ac.th', | |
'ac.tz', | |
'ac.ug', | |
'ac.uk', | |
'ac.yu', | |
'ac.za', | |
'ac.zm', | |
'ac.zw', | |
'edu', | |
'edu.af', | |
'edu.al', | |
'edu.ar', | |
'edu.au', | |
'edu.az', | |
'edu.ba', | |
'edu.bb', | |
'edu.bd', | |
'edu.bh', | |
'edu.bi', | |
'edu.bn', | |
'edu.bo', | |
'edu.br', | |
'edu.bs', | |
'edu.bt', | |
'edu.bz', | |
'edu.co', | |
'edu.cu', | |
'edu.do', | |
'edu.dz', | |
'edu.ec', | |
'edu.ee', | |
'edu.eg', | |
'edu.er', | |
'edu.es', | |
'edu.et', | |
'edu.ge', | |
'edu.gh', | |
'edu.gr', | |
'edu.gt', | |
'edu.hk', | |
'edu.hn', | |
'edu.ht', | |
'edu.iq', | |
'edu.jm', | |
'edu.jo', | |
'edu.kg', | |
'edu.kh', | |
'edu.kn', | |
'edu.kw', | |
'edu.ky', | |
'edu.kz', | |
'edu.la', | |
'edu.lb', | |
'edu.lv', | |
'edu.ly', | |
'edu.mk', | |
'edu.mm', | |
'edu.mn', | |
'edu.mo', | |
'edu.mt', | |
'edu.mx', | |
'edu.my', | |
'edu.ni', | |
'edu.np', | |
'edu.om', | |
'edu.pa', | |
'edu.pe', | |
'edu.ph', | |
'edu.pk', | |
'edu.pl', | |
'edu.pr', | |
'edu.ps', | |
'edu.pt', | |
'edu.py', | |
'edu.qa', | |
'edu.rs', | |
'edu.ru', | |
'edu.sa', | |
'edu.sd', | |
'edu.sg', | |
'edu.sv', | |
'edu.sy', | |
'edu.tr', | |
'edu.tt', | |
'edu.tw', | |
'edu.ua', | |
'edu.uy', | |
'edu.ve', | |
'edu.vn', | |
'edu.ws', | |
'edu.ye', | |
'edu.zm', | |
'vic.edu.au' | |
] | |
end |
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
module HashDataStruct | |
ACADEMIC_TLDS = { | |
'ac.ae' => 1, | |
'ac.at' => 1, | |
'ac.bd' => 1, | |
'ac.be' => 1, | |
'ac.cr' => 1, | |
'ac.cy' => 1, | |
'ac.fj' => 1, | |
'ac.id' => 1, | |
'ac.il' => 1, | |
'ac.ir' => 1, | |
'ac.jp' => 1, | |
'ac.ke' => 1, | |
'ac.kr' => 1, | |
'ac.ma' => 1, | |
'ac.mu' => 1, | |
'ac.mw' => 1, | |
'ac.mz' => 1, | |
'ac.nz' => 1, | |
'ac.pa' => 1, | |
'ac.pg' => 1, | |
'ac.rs' => 1, | |
'ac.ru' => 1, | |
'ac.rw' => 1, | |
'ac.th' => 1, | |
'ac.tz' => 1, | |
'ac.ug' => 1, | |
'ac.uk' => 1, | |
'ac.yu' => 1, | |
'ac.za' => 1, | |
'ac.zm' => 1, | |
'ac.zw' => 1, | |
'edu' => 1, | |
'edu.af' => 1, | |
'edu.al' => 1, | |
'edu.ar' => 1, | |
'edu.au' => 1, | |
'edu.az' => 1, | |
'edu.ba' => 1, | |
'edu.bb' => 1, | |
'edu.bd' => 1, | |
'edu.bh' => 1, | |
'edu.bi' => 1, | |
'edu.bn' => 1, | |
'edu.bo' => 1, | |
'edu.br' => 1, | |
'edu.bs' => 1, | |
'edu.bt' => 1, | |
'edu.bz' => 1, | |
'edu.co' => 1, | |
'edu.cu' => 1, | |
'edu.do' => 1, | |
'edu.dz' => 1, | |
'edu.ec' => 1, | |
'edu.ee' => 1, | |
'edu.eg' => 1, | |
'edu.er' => 1, | |
'edu.es' => 1, | |
'edu.et' => 1, | |
'edu.ge' => 1, | |
'edu.gh' => 1, | |
'edu.gr' => 1, | |
'edu.gt' => 1, | |
'edu.hk' => 1, | |
'edu.hn' => 1, | |
'edu.ht' => 1, | |
'edu.iq' => 1, | |
'edu.jm' => 1, | |
'edu.jo' => 1, | |
'edu.kg' => 1, | |
'edu.kh' => 1, | |
'edu.kn' => 1, | |
'edu.kw' => 1, | |
'edu.ky' => 1, | |
'edu.kz' => 1, | |
'edu.la' => 1, | |
'edu.lb' => 1, | |
'edu.lv' => 1, | |
'edu.ly' => 1, | |
'edu.mk' => 1, | |
'edu.mm' => 1, | |
'edu.mn' => 1, | |
'edu.mo' => 1, | |
'edu.mt' => 1, | |
'edu.mx' => 1, | |
'edu.my' => 1, | |
'edu.ni' => 1, | |
'edu.np' => 1, | |
'edu.om' => 1, | |
'edu.pa' => 1, | |
'edu.pe' => 1, | |
'edu.ph' => 1, | |
'edu.pk' => 1, | |
'edu.pl' => 1, | |
'edu.pr' => 1, | |
'edu.ps' => 1, | |
'edu.pt' => 1, | |
'edu.py' => 1, | |
'edu.qa' => 1, | |
'edu.rs' => 1, | |
'edu.ru' => 1, | |
'edu.sa' => 1, | |
'edu.sd' => 1, | |
'edu.sg' => 1, | |
'edu.sv' => 1, | |
'edu.sy' => 1, | |
'edu.tr' => 1, | |
'edu.tt' => 1, | |
'edu.tw' => 1, | |
'edu.ua' => 1, | |
'edu.uy' => 1, | |
'edu.ve' => 1, | |
'edu.vn' => 1, | |
'edu.ws' => 1, | |
'edu.ye' => 1, | |
'edu.zm' => 1, | |
'vic.edu.au' => 1, | |
} | |
end |
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 'benchmark' | |
require_relative 'hash_data_struct' | |
require_relative 'list_data_struct' | |
number_of_tests = ARGV.first.to_i | |
number_of_tests = 100 if number_of_tests == 0 | |
hash_time = Benchmark.measure do | |
number_of_tests.times do | |
ArrayDataStruct::ACADEMIC_TLDS.each do |edu| | |
HashDataStruct::ACADEMIC_TLDS[edu] | |
HashDataStruct::ACADEMIC_TLDS["#{ edu }.foo"] | |
end | |
end | |
end | |
array_time = Benchmark.measure do | |
number_of_tests.times do | |
ArrayDataStruct::ACADEMIC_TLDS.each do |edu| | |
ArrayDataStruct::ACADEMIC_TLDS.include?(edu) | |
ArrayDataStruct::ACADEMIC_TLDS.include?("#{ edu }.foo") | |
end | |
end | |
end | |
puts 'For the Hash:' | |
puts hash_time | |
puts 'For the Array:' | |
puts array_time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment