Created
July 10, 2009 18:08
-
-
Save norman/144664 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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