搜索
首页php教程php手册smarty模板嵌套之include与fetch性能测试

方法一、使用在父模板中使用{include file="child.tpl"}直接将子模板包含进来

  优点:

    1、有利于模块的划分和模板的重用。

    2、嵌套层次不多的时候,模板的结构清晰,一眼过去就知道这个模板的内容和结构。

    3、只需要一个smarty实例就能做完所有的事情,减少系统资源的占用。

  不足:

    1、子模板中的变量可能与父模板的变量发生冲突

    2、多重嵌套的时候,变量名冲突的几率增大,为所有的变量赋值的难度也加大。

    3、子模板的可操控性差,例如不能通过设置$cache_id, $compile_id,来得到多个结果。

    4、扩展性差。如果需要添加新的模板的时候需要修改模板,同时还需要修改程序。

方法二、通过$smarty->fetch()将子模板的数据当成变量assign到父模板中

  优点:

    1、同样有利于模块的划分,模板的重用

    2、模板的结构同样清晰

    3、子模板作为单独的对象,可操控性强

    4、扩展性较好

    5、每个子模板都是单独的对象,不会出现变量名冲突的问题

  不足:

    1、如果子模板多的时候,需使用多个smarty实例来实现,PHP代码显得臃肿

    2、占用较多的系统资源

    3、......

在那个的时候,出于对性能的要求,以及在没有测试的情况下,想当然的认为include的性能一定就比fetch的性能要高,所有本人使用include这个方法,结果随着系统功能的不断的增加,感觉到越写越复杂,这有很大一部分原因是设计的问题,但是模板间的嵌套也是问题之一,于是回过头来考虑使用fetch这个方法。在重新做出决定之前对这个方法的性能做了测试,分别对单个模板的情况下和多个模板的情况进行测试

单个子模板测试:

有三个页面test1.php,test2.php,test3.php。这三个页面都输出同样的内容。test1.php的模板没有嵌套,test2.php的模板使用了一次include,test3.php使用fetch来实现子模板的嵌套。使用apache自带的ab测试访问各个页面所需要的平均时间(单位:ms)

第一次测试时,使用ab -n 10000 对以上三个页面进行10次,每次累计10000个访问,结果如下。

ab -n 10000 test1.php test2.php test3.php test2/test1 test3/test1
1 5.178 5.442 6.097 1.050984936 1.177481653
2 5.253 5.972 6.027 1.136874167 1.147344375
3 5.223 6.039 5.987 1.156232051 1.146276087
4 5.533 5.997 6.02 1.083860474 1.08801735
5 5.557 6.308 6.03 1.135144862 1.085117869
6 5.248 6.002 5.998 1.14367378 1.142911585
7 5.211 5.933 6.003 1.138553061 1.151986183
8 5.303 6.031 6.048 1.137280784 1.140486517
9 5.213 5.923 6.033 1.136197967 1.15729906
总计 47.719 53.647 54.243 1.124227247 1.136717031

第二次测试时,使用ab -n 100000 对以上三个页面进行1次,每次累计100000个访问,结果如下。

ab -n 100000 test1.php test2.php test3.php test2/test1 test3/test1
1 5.723 7.874 8.55 1.375851826 1.493971693

第一次测试的数据中,test1和test2的波动比较明显,test3则比较稳定,这个现象让我感到十分的奇怪。从平均数据来看,测试结果还是在预期之内的,test1的性能最好,test2比test1多耗费了12.42%的时间,test3比test1多耗费了13.67%的时间,但是test2和test3之间的差距并不大,仅仅只有1.3%左右的差别。

第二次测试的结果,三个页面的执行时间比为1:1.37:1.49,奇怪的是这次的波动更大。。。

多个子模板测试:

同样有三个页面test1.php,test2.php,test3.php。这三个页面都输出同样的内容。test1.php的模板没有嵌套,test2.php的模板使用了10次include嵌入了10个子模板,test3.php使用10次fetch嵌入了10个子模板。使用apache自带的ab测试访问各个页面所需要的平均时间(单位:ms)

因为时间的关系我只进行了一次ab -n 50000的测试,数据如下

ab -n 50000 test1.php test2.php test3.php test2/test1 test3/test1
1 5.68 6.054 6.028 1.06584507 1.061267606

虽然测试的方法和工具可能不是很好,但是通过这些数据的对比,在实际的应用中到底要采用哪种方法,我想大家应该心里有底了吧。

不足和错误之处还望指出,希望大家能多讨论多交流。



声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
Apple iPhone 16 Pro as Leica smartphone: Leica subsidiary confirms camera grip with official Leica appApple iPhone 16 Pro as Leica smartphone: Leica subsidiary confirms camera grip with official Leica appJun 13, 2024 pm 08:52 PM

LeicareleasedtheLeicaLuxcameraappfortheAppleiPhoneafewdaysago.However,theappwasnotdevelopedbyLeica,butbyFjorden.ThecompanyhasbeenknownprimarilyforitscameragripsfortheiPhoneandwasacquiredbyLeicainDecember2023.Fo

Remote 3 universal remote control comes with touchscreen, but without subscription or server obligationRemote 3 universal remote control comes with touchscreen, but without subscription or server obligationJun 14, 2024 am 09:13 AM

SincethedemiseofLogitech'spopularHarmonyremotecontrols,themarketforhigh-qualityuniversalremotecontrolshasbeenfragmentedatbest.UnfoldedCircleaimstoavoidthefateoftheHarmonyUltimatebyeliminatinganyserverobligationsorsubs

Samsung Galaxy S24 Ultra gains E Ink secondary display via inventive protective caseSamsung Galaxy S24 Ultra gains E Ink secondary display via inventive protective caseJun 14, 2024 am 10:44 AM

Anintriguingthird-partycasefortheGalaxyS24Ultra(curr.$1,099.99onAmazon)hasappearedonmarketplaceslikeAliExpress.Astheimagesthroughoutthisarticleshow,thecasehasasimplesiliconeconstruction.However,italsocontainsanEInkd

Light Phone 3 launches with 50% discount, monochrome OLED and minimalist designLight Phone 3 launches with 50% discount, monochrome OLED and minimalist designJun 13, 2024 pm 10:18 PM

WhiletheLightPhone2from2018wasstillequippedwithaneconomicale-inkdisplay,theLightPhone3usesanOLEDdisplaythatcanonlydisplaygrayscale.Thereasonfortheswitchtothe3.92-inchOLEDpanelwithitsresolutionof1,240x1,080isth

Electric Ferraris will boast an \'authentic\' growl, claims marketing executiveElectric Ferraris will boast an \'authentic\' growl, claims marketing executiveJun 14, 2024 am 10:26 AM

In2012,theeminentautomotivejournalistJeremyClarksonstatedthattheLamborghiniAventador,whichhewasreviewingatthetime,wouldbeamongthelastcarstofeatureanaturallyaspiratedV12.Morethanadecadelater,V12-poweredsupercarsareal

Jabra discontinues Elite wireless earbuds lineupJabra discontinues Elite wireless earbuds lineupJun 14, 2024 am 09:39 AM

TheJabraEliteserieshascometoanend,withGNCEOPeterKarlstromerannouncingthediscontinuationofthewirelessearbudswithinthelineupinapressrelease.Petersaysthatthechangingmarketdynamicsandhowchallengingithasbecometogetas

Audio-Technica unveils ATH-S300BT wireless headphones with hear-through noise-cancellation, 90 hours battery life, and multipoint pairingAudio-Technica unveils ATH-S300BT wireless headphones with hear-through noise-cancellation, 90 hours battery life, and multipoint pairingJun 14, 2024 am 09:46 AM

Audio-TechnicahasunveiledtheATH-S300BTwirelessheadphoneswithhear-throughnoise-cancellation,multipointpairing,and90hoursofbatterylife.Thenoise-cancellingfeaturehasthreemodes:off,on,andhear-through,whereambientsoundscanbehea

First complete PCIe 7.0 IP solution presented by Synopsys coming to market in 2025 for HPCs and AI supercomputersFirst complete PCIe 7.0 IP solution presented by Synopsys coming to market in 2025 for HPCs and AI supercomputersJun 14, 2024 am 09:19 AM

Backin2022,whenPCIe7.0wasstartingtotakeshapeasafuturestandard,PCIe5.0wasjusthittingtheservermarketsandtheprospectofseeingPCIe6.0devicesavailableanytimesoon,letalonePCIe7.0ones,wasquitefar-fetched.CurrentlyPCIe5

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前By尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。