Skip to content

Instantly share code, notes, and snippets.

@colinsurprenant
Last active August 29, 2015 14:07
Show Gist options
  • Save colinsurprenant/9a401dacaa15381e2ded to your computer and use it in GitHub Desktop.
Save colinsurprenant/9a401dacaa15381e2ded to your computer and use it in GitHub Desktop.
# encoding: utf-8
require "securerandom"
require "benchmark"
COUNT = 10000000
STRINGS = COUNT.times.map{|i| SecureRandom.hex}
Benchmark.bmbm(30) do |b|
b.report("force_encoding") do
tmp = ""
STRINGS.each do |s|
tmp = s.dup.force_encoding(Encoding::ASCII_8BIT)
end
end
b.report("byteslice") do
tmp = ""
STRINGS.each do |s|
tmp = s.dup.byteslice(0, s.bytesize)
end
end
end
ruby -J-Xmx8g tmp/perf/bytes.rb
Rehearsal ------------------------------------------------------------------
force_encoding 23.210000 0.550000 23.760000 ( 8.623000)
byteslice 1.380000 0.000000 1.380000 ( 1.153000)
-------------------------------------------------------- total: 25.140000sec
user system total real
force_encoding 1.410000 0.010000 1.420000 ( 1.322000)
byteslice 1.230000 0.000000 1.230000 ( 1.107000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment