Python 最快的模板系统是什么?

发布于 2024-08-02 23:17:23 字数 82 浏览 4 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北陌 2024-08-09 23:17:23

以下是流行的模板引擎渲染 10x1000 HTML 表格的结果。

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 性能测试的代码< /a> 添加了一些模板引擎并添加了迭代以提高准确性。最后的列表和生成器 concat 是手动编码的 Python,以了解通过编译为 Python 字节码可实现的性能上限。优化版本在内循环中使用字符串插值。

但在你开始更换模板引擎之前,请确保它很重要。在编译模板引擎之间的差异开始变得重要之前,您需要进行一些相当大的缓存和真正优化的代码。对于大多数应用程序来说,良好的抽象设施、与设计工具的兼容性、熟悉程度和其他因素更为重要。

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

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.

凶凌 2024-08-09 23:17:23

jinja2 文档 来看,如果这就是您所需要的,那么 string.Template 似乎是最快的。

毫无疑问你应该尝试
从模板中删除尽可能多的逻辑
可能的。但模板没有任何
逻辑意味着你必须做所有的事情
代码中的处理很无聊
又愚蠢。一个模板引擎
它是随 Python 一起提供的吗?
称为字符串.模板。不带
循环和 if 条件 and is 到目前为止
最快的模板引擎
获取Python。

From the jinja2 docs, it seems that string.Template is the fastest if that's all you need.

Without a doubt you should try to
remove as much logic from templates as
possible. But templates without any
logic mean that you have to do all the
processing in the code which is boring
and stupid. A template engine that
does that is shipped with Python and
called string.Template. Comes without
loops and if conditions and is by far
the fastest template engine you can
get for Python.

不气馁 2024-08-09 23:17:23

如果您可以将缓存混合在一起(例如 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.

ぽ尐不点ル 2024-08-09 23:17:23

一般来说,您必须进行分析才能回答该问题,因为这取决于您如何使用模板以及用途。

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.

稀香 2024-08-09 23:17:23

我认为 Cheetah 可能是最快的,因为它是用 C 实现的。

I think Cheetah might be the fastest, as it's implemented in C.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文