Skip to content

Instantly share code, notes, and snippets.

@norman
Created July 10, 2009 18:08
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 norman/144664 to your computer and use it in GitHub Desktop.
Save norman/144664 to your computer and use it in GitHub Desktop.
require 'luarocks.require'
require 'rex'
require 'socket'
-- returns tag, id and classes
function parse_haml_tag_using_regex(str)
return rex.match(str, '(%[a-z0-9]*)?(#[a-z0-9-_]*)?(\.[a-z0-9-_\.]*)?')
end
-- returns tag, id and classes
function parse_haml_tag_using_patterns(str)
return string.match(str, '(%%[%w]*)'),
string.match(str, '(#[%w-_]*)'),
string.match(str, '(%.[%w-_%.]*)')
end
function benchmark(str, repetitions, func)
local start_time = socket.gettime() * 1000
for i = 0, repetitions do
func(str)
end
return socket.gettime() * 1000 - start_time
end
local str = "%h2#my_id.class1.class2.class3"
print("Patterns: " .. benchmark(str, 10000, parse_haml_tag_using_patterns) .. " milliseconds")
print("Regex: " .. benchmark(str, 10000, parse_haml_tag_using_regex) .. " milliseconds")
-- on my machine:
-- Patterns: 32.72509765625 milliseconds
-- Regex: 63.48095703125 milliseconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment