Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't this Jinja2 template rendering faster than Djangos?

I was curious to see how much faster Jinja2 (2.6) was than the stock Django (1.3.1) template engine.

Running it I get:

Django: 275.729 ms per iteration
Jinja2: 281.190 ms per iteration

(the smaller the better)

Here's the Django benchmark: http://hastebin.com/DyGcxEybYd.py

Here's the Jinja2 benchmark: http://hastebin.com/uorDENWrkM.py

For a reference, the same Tornado template test manages to do it in 28.127 ms per iteration which is about 10 times faster which is almost too good to be true.

Same Tornado benchmark: http://hastebin.com/F9PcqGb2sZ.py

UPDATE

Unfortunately the explanation is that OSX is unreliable to do benchmarks on. Could just be the OS or could be that I'm running a bunch of other GUI apps such as browsers with far too many tabs. Trying all of these again on a Debian server under very low load I get these numbers:

(manually rounded from having run it many times over a long period)
Django: 475 ms per iteration
Jinja2: 16 ms per iteration 
Tornado: 50 ms per iteration

My work environment is OSX but servers are all Linuxy so this satisfies me.

like image 639
Peter Bengtsson Avatar asked Nov 29 '11 23:11

Peter Bengtsson


1 Answers

I can't reproduce your results. I used your test files and generated a blank Django project with

django-admin.py startproject foo && cd foo

With Tornado 2.1.1, I get a consistent 45ms per iteration. With Django 1.3.1, I get 480ms per iteration.

For Jinja2 I ran 4 tests. 2.5.2 and 2.6 with and without MarkupSafe:

2.5.2:

  • w/ MarkupSafe: 19ms
  • w/o: 14ms

2.6:

  • w/: 14ms
  • w/o: 15ms

It's interesting to me that MarkupSafe actually slows down 2.5.2, significantly (though still only 5ms), though that might be because I don't have the version of MarkupSafe that was contemporary with Jinja2 2.5.2.

Your test also doesn't use the Jinja2 byte-code cache, which might significantly speed it up. I'm not sure if Django has a template code cache or not--I know it has fragment caching but I'm not sure about the whole thing.


Update: I tried the built-in MemcachedBytecodeCache and it slowed down Jinja2 2.6 to 22ms, probably because on a template this simple, the network activity is worse than building it. With an in-process memory cache, it was the same 14ms.

like image 160
James Socol Avatar answered Nov 15 '22 03:11

James Socol