template = jinja2.Template(
    """
    {% for i in RANGE1 %}<h1>hello world {{ i}}</h1>{% endfor %}
    {% for i in RANGE2 %}<h1>foo bar {{ i}}</h1>{% endfor %}
    {{ SLOW() }}
        """
    )
pr.enable()
context = {'RANGE1': list(range(1000000)), 'RANGE2': list(range(100)), 'SLOW': slow}
template.render(context)
pr.disable()
s = StringIO.StringIO()
ps = pstats.Stats(pr, stream=s).sort_stats('cumulative')
ps.print_stats()
print(s.getvalue())
How to profile a Jinja2 template?