Skip to content

Instantly share code, notes, and snippets.

@evaneykelen
Last active August 25, 2019 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evaneykelen/8c455e016afd33447dc33b1c17c887bc to your computer and use it in GitHub Desktop.
Save evaneykelen/8c455e016afd33447dc33b1c17c887bc to your computer and use it in GitHub Desktop.
`size` vs `count` benchmark
require 'benchmark'
array = File.readlines('dutch.bin') # 10k lines
hash = Hash[*array] # 5k keys => 5k values
Benchmark.bmbm do |x|
x.report {
x.report("Array count") { 10_000.times { array.count } }
x.report("Array size") { 10_000.times { array.size } }
x.report("Hash count") { 10_000.times { hash.count } }
x.report("Hash size") { 10_000.times { hash.size } }
}
end
@evaneykelen
Copy link
Author

Rehearsal ------------------------------------
   0.000008   0.000001   0.000009 (  0.000005)
Array count  0.000612   0.000002   0.000614 (  0.000613)
Array size  0.000455   0.000000   0.000455 (  0.000456)
Hash count  1.926249   0.001206   1.927455 (  1.928529)
Hash size  0.000514   0.000023   0.000537 (  0.000572)
--------------------------- total: 1.929070sec

       user     system      total        real
   0.000014   0.000004   0.000018 (  0.000028)
Array count  0.000630   0.000001   0.000631 (  0.000628)
Array size  0.000450   0.000001   0.000451 (  0.000449)
Hash count  1.913865   0.000813   1.914678 (  1.915393)
Hash size  0.000501   0.000028   0.000529 (  0.000549)
Array count  0.000628   0.000002   0.000630 (  0.000626)
Array size  0.000449   0.000001   0.000450 (  0.000448)
Hash count  1.914921   0.000986   1.915907 (  1.916950)
Hash size  0.000488   0.000036   0.000524 (  0.000548)

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