Why is this Jinja2 template not faster than Djangos?

I was curious to know how much faster Jinja2 (2.6) than the Jango template engine (1.3.1).

Running it, I get:

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

(less is better)

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

Here's a Jinja2 test score: http://hastebin.com/uorDENWrkM.py

For reference, the same Tornado template test does this at 28.127 ms per iteration , which is about 10 times faster, which is almost too good to be true.

The same Tornado tests: http://hastebin.com/F9PcqGb2sZ.py

UPDATE

Unfortunately, the explanation is that OSX is not reliable to run tests. Maybe it's just an OS, or it may be that I am launching many other graphical applications, such as browsers with too many tabs. Repeating all this again on a Debian server at a very low load, I get the following 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 working environment is OSX, but the servers are all Linuxy, so that satisfies me.

+6
source share
1 answer

I can not reproduce your results. I used your test files and created an empty Django project with

 django-admin.py startproject foo && cd foo 

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

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

2.5.2

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

2.6

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

I'm curious that MarkupSafe actually slows down 2.5.2, significantly (although it is still only 5 ms), although that may be because I don't have the version of MarkupSafe that was modern with Jinja2 2.5.2.

Your test also does not use Jinja2 cache code, which can significantly speed it up. I'm not sure if Django has a template cache or not - I know that it has fragment caching, but I'm not sure about that.


Update: I tried the built-in MemcachedBytecodeCache , and it slowed down Jinja2 2.6 to 22ms, perhaps because the template is simple, network activity is worse than creating it. With a cache in RAM, these were the same 14 ms.

+3
source

Source: https://habr.com/ru/post/902710/


All Articles