def test(pattern, path, count): print 'fnmatch(%s, %s) %d times...' % (path, pattern, count) start_compile = datetime.now() compiled = Compiled(DFA(NFA.fnmatch(pattern))) compiled.optimize() # slow! end_compile = datetime.now() start_execution = datetime.now() for x in range(count): compiled(path) end_execution = datetime.now() native_test = compile_native_test(compiled, pattern, path, count) start_native = datetime.now() native_test() end_native = datetime.now() from fnmatch import fnmatch start_builtin = datetime.now() for x in range(count): fnmatch(path, pattern) end_builtin = datetime.now() return { 'pattern': pattern, 'path': path, 'count': count, 'compile_time': (end_compile-start_compile), 'execution_time': (end_execution-start_execution), 'native_time': (end_native-start_native), 'builtin_time': (end_builtin-start_builtin), }
#!/usr/bin/env python # our code from dfa import DFA from nfa import NFA from compiler import Compiled # the python implementation import fnmatch # run unit self tests from characterset import test_CharacterSet test_CharacterSet() from dfa import test_distinctArcs test_distinctArcs() PATTERNS = ('*.txt', '*', '*.*', 'README.*') PATHS = ('test.c', 'README.txt', 'README') for pattern in PATTERNS: nfa = NFA.fnmatch(pattern) dfa = DFA(nfa) compiled = Compiled(dfa, debug=False) compiled.optimize() for path in PATHS: expected = fnmatch.fnmatch(path, pattern) assert nfa(path) == expected assert dfa(path) == expected assert compiled(path) == expected