Skip to content

Instantly share code, notes, and snippets.

@billdueber
Last active December 11, 2015 11:48
Show Gist options
  • Save billdueber/4596330 to your computer and use it in GitHub Desktop.
Save billdueber/4596330 to your computer and use it in GitHub Desktop.
File.read vs File.read with size
require 'benchmark'
bigfiles = %w[ddd1 ddd2 ddd3] # all copies of the same 6MB file
puts RUBY_DESCRIPTION
puts "Bigfile size is ", File.size('ddd1')
Benchmark.bmbm do |bm|
bm.report("straight read") { bigfiles.each {|bigfile| File.read(bigfile) } }
bm.report("read w/ size") { bigfiles.each {|bigfile| File.read(bigfile,File.size(bigfile)) } }
end
#=> jruby 1.7.2 (1.9.3p327) 2013-01-04 302c706 on Java HotSpot(TM) 64-Bit Server VM 1.8.0-ea-b55 +indy [darwin-x86_64]
#=> Bigfile size is 6200808
#=> Rehearsal -------------------------------------------------
#=> straight read 0.320000 0.050000 0.370000 ( 0.203000)
#=> read w/ size 0.030000 0.000000 0.030000 ( 0.023000)
#=> ---------------------------------------- total: 0.400000sec
#=>
#=> user system total real
#=> straight read 0.190000 0.010000 0.200000 ( 0.137000)
#=> read w/ size 0.010000 0.010000 0.020000 ( 0.012000)
### Faster is jruby --1.8
#=> jruby 1.7.2 (ruby-1.8.7p370) 2013-01-04 302c706 on Java HotSpot(TM) 64-Bit Server VM 1.8.0-ea-b55 +indy [darwin-x86_64]
#=> Bigfile size is 6200808
#=> Rehearsal -------------------------------------------------
#=> straight read 0.080000 0.010000 0.090000 ( 0.037000)
#=> read w/ size 0.040000 0.000000 0.040000 ( 0.025000)
#=> ---------------------------------------- total: 0.130000sec
#=>
#=> user system total real
#=> straight read 0.030000 0.010000 0.040000 ( 0.024000)
#=> read w/ size 0.010000 0.000000 0.010000 ( 0.012000)
# Much, much faster in MRI
#=> ruby 1.9.3p362 (2012-12-25 revision 38607) [x86_64-darwin11.4.2]
#=> Bigfile size is 6200808
#=> Rehearsal -------------------------------------------------
#=> straight read 0.000000 0.000000 0.000000 ( 0.009195)
#=> read w/ size 0.010000 0.010000 0.020000 ( 0.010848)
#=> ---------------------------------------- total: 0.020000sec
#=>
#=> user system total real
#=> straight read 0.000000 0.010000 0.010000 ( 0.009717)
#=> read w/ size 0.000000 0.010000 0.010000 ( 0.009302)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment