Source

web2py-template / timetest.py

import timeit

code_to_parse = '''{{if something:
some_code
other_code
elif other_condition:
elif_code
other_elif_code
else:
code_on_else
other_code_on_else
pass
code_outside
for a in b:
code_on_for
other_code
pass
some_code_outside
for c in d:
code_on_second_for
other_code_on_second_for
else:
code_on_else
other_code_on_else
pass
other_code_outside
try:
something
code_to_try
except:
code_on_the_exception
blablabla
pass
code_outside
try:
other_try_code
more_one_line
except SomeException, e:
lets_solve_this_exception
more_code
except:
code_to_general_exception
goes_here
pass
more_code_outside_all_blocks}}'''

t1 = timeit.Timer('parse_template(code_to_parse)',
                 '''from new_template import parse_template
from __main__ import code_to_parse''').timeit(number=1000)
print t1

t2 = timeit.Timer('parse(code_to_parse)',
                 '''from template import parse
from __main__ import code_to_parse''').timeit(number=1000)
print t2

print t1 / t2