纯 PHP/HTML 视图 VS 模板引擎视图视图、模板、引擎、PHP

2023-09-06 23:54:42 作者:静待星辰落

我想知道哪种方法更快,在 HTML 文件中使用纯 PHP 或使用 Smarty、Twig 等模板引擎...我特别想知道的是下一个:解析速度更快,例如 Smarty 缓存是否比使用纯 PHP 更快?哪个模板引擎最快?我即将重写简单的应用程序,其中速度是第一位的.

I would like to know which approach is faster, using the pure PHP in the HTML files or using a template engines like Smarty,Twig, ... What I would particularly like to know is next: which is parsed faster, is the Smarty cache for example faster than using pure PHP? Which of the template engines is the fastest? I'm about to rewrite simple application where speed is on the first place.

推荐答案

取决于"是您所有问题的答案.

"Depends" is the answer to all your questions.

什么是更快"?执行时间处理时间?开发时间?维护?内存开销?它们的混合物?模板引擎通常会牺牲一些性能(速度、内存)以获得更好的开发和维护.

What is "faster"? Execution time? Development time? Maintenance? Memory overhead? A mixture of them? A template engine is usually trading in some performance (speed, memory) for better development and maintenance.

如果您谈论的是纯动态模板(意思是:在每个请求上评估模板),PHP 将超越任何模板引擎.真的,这是一个nobrainer.如果您考虑缓存,Smarty 之类的模板引擎可能会有所帮助.不过,缓存不是您无法在普通 PHP 中实现的东西.有了 Smarty,它就为您完成了(而且比您可能做的要复杂得多).

If you are talking about purely dynamic templating (meaning: template evaluated on every request) PHP will outrun any template engine. This is a nobrainer, really. If you're taking caching into account, a template engine like Smarty may help. Caching is nothing you couldn't implement yourself in plain PHP, though. With Smarty it's just been done for you (and on a far more sophisticated level than you possibly would).

如果您使用框架,比如 Symfony,使用 Twig 可能是明智的,因为 Twig 和 Symfony 紧密集成.当然,您可以使用 Smarty 或纯 PHP.这里的问题是:可行吗?

If you are using a framework, say Symfony, it might be wise to use Twig, as Twig and Symfony are tightly integrated. Sure you can use Smarty or plain PHP. The question here is: is it practicable?

从数据库或远程 API 等数据源构建网站时,缓存很有意义.您真正节省(从某种意义上说)是数据库调用、密集计算等.检查您是否有任何时间密集型功能正在运行来构建您的站点.如果是这样,请使用缓存(如果可以的话).

Caching makes sense when building sites from datasources like a database or remote APIs. What you are really saving (in a sense of reducing) here are database calls, intensive calculations, etc. Check if you have any time-intensive functions running to build your site. If so, use caching (if you can).

了解开发/维护/便利/性能权衡,我会(总是)推荐使用模板引擎.作为 Smarty 开发人员,我当然会建议使用 Smarty.也就是说,除非你使用 Symfony,否则你可能会更好地使用 Twig.或者其他一些具有其他模板引擎的框架.

Knowing development/maintenance/convenience/performance trade-offs, I would (always) recommend using a template engine. Being a Smarty developer, I'll, of course, suggest using Smarty. That is unless you're using Symfony, then you might be better of with Twig. Or some other framework featuring some other template engine.

请忽略 Smarty vs. Twig 之类的帖子,因为它们只比较非常有限的视图的引擎.不要相信您没有伪造自己的基准™.

Please ignore posts like Smarty vs. Twig, as they only compare a very limited view of the engines. Don't trust benchmarks you haven't faked yourself™.

不过,总的来说,Smarty 3.1 比 Twig 快一点.Twig 在运行时(即执行模板的时间)做了很多 Smarty 在编译时(即准备执行模板的时间)所做的事情.Twig 在这里并不是真的很讨厌速度.Twig 需要根据设计在运行时做某些事情.他们用一点性能换取一点方便"(例如,使用相同的符号访问数组和对象).

In general, though, Smarty 3.1 is a bit faster than Twig. Twig is doing a lot of stuff at runtime (being the time when a template is executed) that Smarty does on compile time (being the time when a template is prepared for execution). Twig is not really pissing away speed here. Twig needs to do certain stuff at runtime by design. They traded a bit of performance for a bit of "convenience" (Accessing arrays and objects with the same notation, for example).