Skip to content

Instantly share code, notes, and snippets.

@fasterthanlime
Last active August 29, 2015 14:10
Show Gist options
  • Save fasterthanlime/37d85de5bc4bb77d0187 to your computer and use it in GitHub Desktop.
Save fasterthanlime/37d85de5bc4bb77d0187 to your computer and use it in GitHub Desktop.
Comparing codepoints is faster it seems
require 'benchmark/ips'
def _codepoint(text)
text.each_codepoint do |p|
case p
when 97 then 97
when 98 then 98
when 99 then 99
when 100 then 100
when 101 then 101
when 102 then 102
when 103 then 103
when 104 then 104
when 105 then 105
when 106 then 106
when 107 then 107
when 108 then 108
when 109 then 109
when 110 then 110
when 111 then 111
when 112 then 112
when 113 then 113
when 114 then 114
when 115 then 115
when 116 then 116
when 117 then 117
when 118 then 118
when 119 then 119
when 120 then 120
when 121 then 121
when 122 then 122
end
end
end
def _char(text)
text.each_char do |c|
case c
when "a" then 97
when "b" then 98
when "c" then 99
when "d" then 100
when "e" then 101
when "f" then 102
when "g" then 103
when "h" then 104
when "i" then 105
when "j" then 106
when "k" then 107
when "l" then 108
when "m" then 109
when "n" then 110
when "o" then 111
when "p" then 112
when "q" then 113
when "r" then 114
when "s" then 115
when "t" then 116
when "u" then 117
when "v" then 118
when "w" then 119
when "x" then 120
when "y" then 121
when "z" then 122
end
end
end
text = %Q{ Contrary to popular belief, Lorem Ipsum is not simply random text.
It has roots in a piece of classical Latin literature from 45 BC, making it
over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney
College in Virginia, looked up one of the more obscure Latin words,
consectetur, from a Lorem Ipsum passage, and going through the cites of the
word in classical literature, discovered the undoubtable source. Lorem Ipsum
comes from sections 1.10.32 and 1.10.33 of de Finibus Bonorum et Malorum (The
Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a
treatise on the theory of ethics, very popular during the Renaissance. The
first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in
section 1.10.32.
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for
those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et
Malorum" by Cicero are also reproduced in their exact original form,
accompanied by English versions from the 1914 translation by H. Rackham. }
Benchmark.ips do |x|
x.report("codepoint") { _codepoint(text) }
x.report("char") { _char(text) }
x.compare!
end
Calculating -------------------------------------
codepoint 1.150k i/100ms
char 486.000 i/100ms
-------------------------------------------------
codepoint 11.751k (± 2.1%) i/s - 59.800k
char 4.967k (± 1.7%) i/s - 25.272k
Comparison:
codepoint: 11751.1 i/s
char: 4966.7 i/s - 2.37x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment