Skip to content

Instantly share code, notes, and snippets.

@bradland
Created January 7, 2012 03:30
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 bradland/1573685 to your computer and use it in GitHub Desktop.
Save bradland/1573685 to your computer and use it in GitHub Desktop.
Encoding benchmarks
# bench_encoding.rb
#!/usr/bin/env ruby -wKU
# encoding: UTF-8
require 'benchmark'
p "Encoding is currently: #{__ENCODING__}"
report_width = 32
Benchmark.bm(report_width) do|b|
b.report("UTF-8 to LATIN-1") do
1_000_000.times do |file|
str = "Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house!"
str.force_encoding("ISO-8859-1")
end
end
end
Benchmark.bm(report_width) do|b|
b.report("String assignment only") do
1_000_000.times do |file|
str = "Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house! Señor Brad is in da house!"
end
end
end
# Results
"Encoding is currently: UTF-8"
user system total real
UTF-8 to LATIN-1 0.500000 0.000000 0.500000 ( 0.509272)
user system total real
String assignment only 0.180000 0.000000 0.180000 ( 0.177283)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment