Skip to content

Instantly share code, notes, and snippets.

@tgxworld
Last active August 29, 2015 14:18
Show Gist options
  • Save tgxworld/766759f70c1041e16750 to your computer and use it in GitHub Desktop.
Save tgxworld/766759f70c1041e16750 to your computer and use it in GitHub Desktop.
{["test.rb", 9, :T_STRING]=>[9, 0, 0, 0, 0, 0],
["test.rb", 9, :T_ARRAY]=>[1, 0, 0, 0, 0, 0],
["test.rb", 9, :T_REGEXP]=>[1, 0, 0, 0, 0, 0]}
-------------------------------------------------
{["test.rb", 13, :T_STRING]=>[5, 0, 0, 0, 0, 0],
["test.rb", 13, :T_REGEXP]=>[1, 0, 0, 0, 0, 0]}
Calculating -------------------------------------
Regular 18.013k i/100ms
Regexp new 21.582k i/100ms
-------------------------------------------------
Regular 198.856k (± 3.2%) i/s - 1.009M
Regexp new 240.396k (± 3.7%) i/s - 1.209M
Comparison:
Regexp new: 240395.9 i/s
Regular: 198855.5 i/s - 1.21x slower
require 'objspace'
require 'allocation_tracer'
require 'pp'
require 'benchmark/ips'
ObjectSpace::AllocationTracer.setup(%i{path line type})
a = :content
result = ObjectSpace::AllocationTracer.trace do
/\A#{Regexp.escape(a)}\(\d+[if]?\)\z/
end
result2 = ObjectSpace::AllocationTracer.trace do
Regexp.new("\\A#{Regexp.escape(a)}\\(\\d+[if]?\\)\\z")
end
pp result
puts "\n"
puts "-------------------------------------------------"
puts "\n"
pp result2
puts "\n"
Benchmark.ips do |x|
x.report('Regular') do
/\A#{Regexp.escape(a)}\(\d+[if]?\)\z/
end
x.report('Regexp new') do
Regexp.new("\\A#{Regexp.escape(a)}\\(\\d+[if]?\\)\\z")
end
x.compare!
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment