Skip to content

Instantly share code, notes, and snippets.

@benweint
Created November 8, 2013 06:05
Show Gist options
  • Save benweint/7366881 to your computer and use it in GitHub Desktop.
Save benweint/7366881 to your computer and use it in GitHub Desktop.
String encoding demo
#!/usr/bin/env ruby
require 'benchmark'
length = 512
iterations = 100000
encodings = Encoding.name_list
Benchmark.bmbm do |r|
r.report("encode") do
iterations.times do
encoding = encodings.sample
x = File.read("/dev/urandom", length).force_encoding(encoding)
y = x.dup.force_encoding('ISO-8859-1').encode('UTF-8')
raise 'unequal' unless y.encode('ISO-8859-1').force_encoding(encoding) == x
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment