为什么这个Jinja2模板渲染速度不比Djangos快?

时间:2020-12-27 20:39:59

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

我很想知道Jinja2(2.6)比Django(1.3.1)模板引擎的速度快多少。

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

这是Django基准:http://hastebin.com/DyGcxEybYd.py

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

这是Jinja2的基准: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.

作为参考,相同的Tornado模板测试设法在每次迭代28.127毫秒内完成它,这大约快10倍,这几乎太好了。

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

相同的龙卷风基准: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:

不幸的是,解释是OSX在基准测试上是不可靠的。可能只是操作系统,或者可能是我正在运行一堆其他GUI应用程序,例如具有太多选项卡的浏览器。在极低负载下在Debian服务器上再次尝试所有这些我得到这些数字:

(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.

我的工作环境是OSX,但服务器都是Linuxy,所以这让我很满意。

1 个解决方案

#1


3  

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

我无法重现你的结果。我使用了你的测试文件并生成了一个空白的Django项目

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.

使用Tornado 2.1.1,每次迭代我得到一致的45ms。使用Django 1.3.1,我每次迭代获得480毫秒。

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

对于Jinja2,我进行了4次测试。 2.5.2和2.6有和没有MarkupSafe:

2.5.2:

  • w/ MarkupSafe: 19ms
  • 带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.

有趣的是,MarkupSafe实际上显着减慢了2.5.2(虽然仍然只有5ms),但这可能是因为我没有与Jinja2 2.5.2同时代的MarkupSafe版本。

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.

您的测试也不使用Jinja2字节码缓存,这可能会显着加快速度。我不确定Django是否有模板代码缓存 - 我知道它有片段缓存,但我不确定整个事情。


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.

更新:我尝试了内置的MemcachedBytecodeCache,它将Jinja2的速度降低到了2到22毫秒,可能是因为在模板这么简单,网络活动比构建它更糟糕。使用进程内存缓存,它是相同的14毫秒。

#1


3  

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

我无法重现你的结果。我使用了你的测试文件并生成了一个空白的Django项目

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.

使用Tornado 2.1.1,每次迭代我得到一致的45ms。使用Django 1.3.1,我每次迭代获得480毫秒。

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

对于Jinja2,我进行了4次测试。 2.5.2和2.6有和没有MarkupSafe:

2.5.2:

  • w/ MarkupSafe: 19ms
  • 带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.

有趣的是,MarkupSafe实际上显着减慢了2.5.2(虽然仍然只有5ms),但这可能是因为我没有与Jinja2 2.5.2同时代的MarkupSafe版本。

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.

您的测试也不使用Jinja2字节码缓存,这可能会显着加快速度。我不确定Django是否有模板代码缓存 - 我知道它有片段缓存,但我不确定整个事情。


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.

更新:我尝试了内置的MemcachedBytecodeCache,它将Jinja2的速度降低到了2到22毫秒,可能是因为在模板这么简单,网络活动比构建它更糟糕。使用进程内存缓存,它是相同的14毫秒。