Skip to content

Instantly share code, notes, and snippets.

@eagleas
Created March 18, 2011 14:56
Show Gist options
  • Save eagleas/876202 to your computer and use it in GitHub Desktop.
Save eagleas/876202 to your computer and use it in GitHub Desktop.
# user system total real
# base64: 1.120000 0.030000 1.150000 ( 1.187320)
# pack: 0.170000 0.040000 0.210000 ( 0.207054)
#
require 'benchmark'
require 'base64'
N = 1000
def base64(file)
Base64.encode64(File.open(file, 'rb') {|f| f.read }).gsub(/\n/, '')
end
def pack(file)
[IO.read(file)].pack("m0")
end
def setup
for i in 1..N ; `dd if=/dev/urandom of=#{i}.dat bs=1k count=40 2>&1 1>/dev/null`; end
end
def teardown
`rm -f *.dat`
end
setup
Benchmark.bm(7) do |x|
x.report("base64:") { for i in 1..N; base64("#{i}.dat"); end }
x.report("pack:") { for i in 1..N; pack("#{i}.dat"); end }
end
teardown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment