Python 最快的模板系统是什么?
Jinja2 和 Mako 显然都相当快。
这些与(功能较少但可能足以满足我正在做的事情) string.Template 相比如何?
Jinja2 and Mako are both apparently pretty fast.
How do these compare to (the less featured but probably good enough for what I'm doing) string.Template ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
以下是流行的模板引擎渲染 10x1000 HTML 表格的结果。
该基准测试基于来自 Spitfire 性能测试的代码< /a> 添加了一些模板引擎并添加了迭代以提高准确性。最后的列表和生成器 concat 是手动编码的 Python,以了解通过编译为 Python 字节码可实现的性能上限。优化版本在内循环中使用字符串插值。
但在你开始更换模板引擎之前,请确保它很重要。在编译模板引擎之间的差异开始变得重要之前,您需要进行一些相当大的缓存和真正优化的代码。对于大多数应用程序来说,良好的抽象设施、与设计工具的兼容性、熟悉程度和其他因素更为重要。
Here are the results of the popular template engines for rendering a 10x1000 HTML table.
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.
从 jinja2 文档 来看,如果这就是您所需要的,那么 string.Template 似乎是最快的。
From the jinja2 docs, it seems that string.Template is the fastest if that's all you need.
如果您可以将缓存混合在一起(例如 memcached),那么根据功能和易用性而不是优化进行选择。
我使用 Mako 是因为我喜欢它的语法和功能。幸运的是,它也是最快的之一。
If you can throw caching in the mix (like memcached) then choose based on features and ease of use rather than optimization.
I use Mako because I like the syntax and features. Fortunately it is one of the fastest as well.
一般来说,您必须进行分析才能回答该问题,因为这取决于您如何使用模板以及用途。
string.Template 是最快的,但太原始了,很难与其他模板系统相提并论,因为它只进行字符串替换,没有条件或循环,这使得它在实践中几乎没有用处。
In general you will have to do profiling to answer that question, as it depends on how you use the templates and what for.
string.Template is the fastest, but so primitive it can hardly be called a template in the same breath as the other templating systems, as it only does string replacements, and has no conditions or loops, making it pretty useless in practice.
我认为 Cheetah 可能是最快的,因为它是用 C 实现的。
I think Cheetah might be the fastest, as it's implemented in C.