Rails 3.2 - haml 与 erb。哈姆尔更快吗? (2012 年 2 月)

发布于 2025-01-05 01:59:13 字数 190 浏览 1 评论 0原文

我正在开发一个项目,并且仍在考虑使用 HAML(漂亮的代码,较小的视图文件大小)而不是经典的 ERB 模板。

我担心为什么我还没有这样做是生成视图的速度 - 我读了一篇文章/基准测试,几乎总是 HAML 比 ERB 慢 - 但事实是,这些文章已经有 2-3 年了。

所以我的问题是,在 2012 年初,这两个模板系统的比较看起来如何?

I am working on a project and I am still thinking about using HAML (beautiful code, less size of view files) instead of classic ERB template.

My worries why I didn't do it yet are the speed of generating views - I read an articles/benchmarks and there was almost always HAML slower than ERB - but the truth is, that the articles are 2-3 years old.

So my question is, how looks the comparison these two template systems now, in the start of 2012?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

柳絮泡泡 2025-01-12 01:59:13

以下是2011 年 11 月的一些基准测试。您应该能够cd到您的haml目录并运行rake benchmark。我说“应该”是因为您需要安装正确的依赖项才能运行该任务。我玩了几分钟,但在本地没有运气。相关文件是haml/test/benchmark.rb

编辑:
我确实为你找到了更多信息。其他人在尝试让基准在本地运行时遇到了与我同样的麻烦,所以他们推出了自己的基准。我分叉了这个要点,并添加了对比较 haml 和 erb 的支持: https://gist.github.com/1807036。我使用了 haml 库用于基准测试的相同模板。以下是我运行代码的结果:

$ ruby benchmark.rb
       user     system      total        real
haml:  0.650000   0.000000   0.650000 (  0.651584)
erb:  0.540000   0.000000   0.540000 (  0.534727)

我使用 ruby​​ 1.9.3-p0,haml 3.1.4。

更新 5/2020:github 上的 @spickermann 指出了基准测试中的一个缺陷,即 haml 与 erb 的比较不公平。 haml 引擎实例仅实例化一次,而 erb 实例在 1000 次基准测试运行中的每一次实例化。当我们修复基准测试并在每次基准测试运行时实例化每个实例时,我们会得到非常不同的结果:

$ ruby benchmark.rb 
       user     system      total        real
haml:  3.259634   0.007936   3.267570 (  3.271728)
erb:  0.205065   0.000571   0.205636 (  0.205824)

这个新基准测试使用 ruby​​ 2.7.1,haml 5.1.2。要点也已更新。

Here are some benchmarks from someone in Nov 2011. You should be able to cd to your haml directory and run rake benchmark. I say "should" because you'll need to get the proper dependencies installed in order to run that task. I played around with it for a few minutes but no luck locally. The relevant file is haml/test/benchmark.rb.

EDIT:
I did find some more information for you. Someone else had the same trouble as me trying to get benchmarks working locally, so they rolled their own. I forked that gist and added support for comparing haml to erb: https://gist.github.com/1807036. I used the same templates that the haml library uses to benchmark. Here are the results that I got running the code:

$ ruby benchmark.rb
       user     system      total        real
haml:  0.650000   0.000000   0.650000 (  0.651584)
erb:  0.540000   0.000000   0.540000 (  0.534727)

I used ruby 1.9.3-p0, haml 3.1.4.

UPDATE 5/2020: @spickermann on github pointed out a flaw in the benchmark, in that the comparison of haml to erb was not fair. The haml engine instance was only instantiated once, whereas the erb instance was instantiated on each of the 1000 benchmark runs. When we fix the benchmark so we instantiate each instance on each benchmark run we get very different results:

$ ruby benchmark.rb 
       user     system      total        real
haml:  3.259634   0.007936   3.267570 (  3.271728)
erb:  0.205065   0.000571   0.205636 (  0.205824)

This new benchmark uses ruby 2.7.1, haml 5.1.2. The gist has also been updated.

请别遗忘我 2025-01-12 01:59:13

网络上已经记录了很多争论。

但是当我们有一个专门使用 ERB 的应用程序时。为什么我们应该尝试转向 HAML?对我来说,这是浪费时间,也是一个糟糕的决定,不会带来很多好处。虽然我们的代码在 HAML 中会突然变得更干净、更漂亮,但我们会浪费宝贵的时间将 ERB 转换为 HAML。我们还有很多其他重要的事情要做,为什么要把时间浪费在如此毫无意义的事情上呢?

这都是相对的。您不需要使用 HAML 来代替 ERB。 ERB 可以做所有同样的事情。它们只是完成同一工作的不同工具。

There are already a bunch of debates documented on the web.

But when We have an application where we used ERB exclusively. Why should we try to make the switch to HAML? To me, it's a waste of time and a poor decision that wouldn't provide many benefits. While our code would suddenly be cleaner and a little more beautiful in HAML, we'd waste precious hours converting ERB to HAML. We have so many other important things to do, why waste that time on something so pointless in this case?

It's all relative. You don't need to use HAML instead of ERB. ERB can do all the same things. They are just different tools for the same job.

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