问题描述
Jinja2 和 Mako 显然都非常快.
Jinja2 and Mako are both apparently pretty fast.
这些与(功能较少但可能足以满足我正在做的事情)string.Template 相比如何?
How do these compare to (the less featured but probably good enough for what I'm doing) string.Template ?
推荐答案
以下是用于渲染 10x1000 HTML 表格的流行模板引擎的结果.
Here are the results of the popular template engines for rendering a 10x1000 HTML table.
Python 2.6.2 on a 3GHz Intel Core 2
Kid template 696.89 ms
Kid template + cElementTree 649.88 ms
Genshi template + tag builder 431.01 ms
Genshi tag builder 389.39 ms
Django template 352.68 ms
Genshi template 266.35 ms
ElementTree 180.06 ms
cElementTree 107.85 ms
StringIO 41.48 ms
Jinja 2 36.38 ms
Cheetah template 34.66 ms
Mako Template 29.06 ms
Spitfire template 21.80 ms
Tenjin 18.39 ms
Spitfire template -O1 11.86 ms
cStringIO 5.80 ms
Spitfire template -O3 4.91 ms
Spitfire template -O2 4.82 ms
generator concat 4.06 ms
list concat 3.99 ms
generator concat optimized 2.84 ms
list concat optimized 2.62 ms
基准测试基于 来自 Spitfire 的代码使用一些添加的模板引擎和添加的迭代进行性能测试以提高准确性.最后的列表和生成器 concat 是手工编码的 Python,以了解通过编译为 Python 字节码可实现的性能上限.优化版本在内循环中使用字符串插值.
The benchmark is based on code from Spitfire performance tests with some added template engines and added iterations to increase accuracy. The list and generator concat at the end are hand coded Python to get a feel for the upper limit of performance achievable by compiling to Python bytecode. The optimized versions use string interpolation in the inner loop.
但是在您用完切换模板引擎之前,请确保它很重要.在编译模板引擎之间的差异开始变得重要之前,您需要进行一些非常繁重的缓存和真正优化的代码.对于大多数应用程序来说,良好的抽象设施、与设计工具的兼容性、熟悉程度和其他因素都更为重要.
But before you run out to switch your template engine, make sure it matters. You'll need to be doing some pretty heavy caching and really optimized code before the differences between the compiling template engines starts to matter. For most applications good abstraction facilities, compatibility with design tools, familiarity and other things matter much much more.
这篇关于Python 最快的模板系统是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!