为什么Jinja2模板的渲染速度没有Django快? [英] Why isn't this Jinja2 template rendering faster than Djangos?
问题描述
我很想知道Jinja2(2.6)比普通的Django(1.3.1)模板引擎快多少.
I was curious to see how much faster Jinja2 (2.6) was than the stock Django (1.3.1) template engine.
运行它,我得到:
Django: 275.729 ms per iteration
Jinja2: 281.190 ms per iteration
(越小越好)
这是Django基准测试: http://hastebin.com/DyGcxEybYd.py
Here's the Django benchmark: http://hastebin.com/DyGcxEybYd.py
这是Jinja2基准: http://hastebin.com/uorDENWrkM.py
Here's the Jinja2 benchmark: http://hastebin.com/uorDENWrkM.py
作为参考,相同的Tornado模板测试设法在28.127 ms per iteration
中进行此操作,该速度大约快10倍,这太好了,难以置信.
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.
相同的龙卷风基准: http://hastebin.com/F9PcqGb2sZ.py
更新
不幸的是,解释是OSX不可靠地进行基准测试.可能只是操作系统,或者可能是我正在运行许多其他GUI应用程序(例如,选项卡太多的浏览器).在非常低的负载下在Debian服务器上再次尝试所有这些操作,我得到以下数字:
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
我的工作环境是OSX,但是服务器都是Linuxy,所以这让我感到满意.
My work environment is OSX but servers are all Linuxy so this satisfies me.
推荐答案
我无法复制您的结果.我使用了您的测试文件,并使用
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
使用Tornado 2.1.1时,每次迭代可以获得一致的 45ms . 使用Django 1.3.1,每次迭代获得 480ms .
With Tornado 2.1.1, I get a consistent 45ms per iteration. With Django 1.3.1, I get 480ms per iteration.
对于Jinja2,我进行了4次测试.具有和不具有 MarkupSafe
For Jinja2 I ran 4 tests. 2.5.2 and 2.6 with and without MarkupSafe:
2.5.2:
- 带有MarkupSafe: 19毫秒
- 不包括: 14ms
- w/ MarkupSafe: 19ms
- w/o: 14ms
2.6:
- w/: 14ms
- 不包括: 15ms
- w/: 14ms
- w/o: 15ms
令我感到有趣的是,实际上MarkupSafe放慢了2.5.2的速度(虽然仍然只有5毫秒),但这可能是因为我没有与Jinja2 2.5.2兼容的MarkupSafe版本.
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.
您的测试也没有使用Jinja2字节码缓存,这可能会大大加快它的速度.我不确定Django是否具有模板代码缓存-我知道它具有片段缓存,但是我不确定整个过程.
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.
更新:我尝试使用内置的MemcachedBytecodeCache
,它将Jinja2的速度降低了2.6到22ms,这可能是因为在这样简单的模板上,网络活动比构建它要差.使用进程内内存缓存时,它是相同的14毫秒.
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.
这篇关于为什么Jinja2模板的渲染速度没有Django快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!