Performance Test of include and fetch nested smarty Templates

Source: Internet
Author: User
When using a template, you will inevitably encounter template nesting. we all know that there are two ways to achieve this. The following are some of my early experiences. Method 1: Use {include file = "child. tpl"} in the parent template to directly include the subtemplate.

Advantages:

1. it is conducive to the division of modules and the reuse of templates.

2. when there are not many nested layers, the template structure is clear and the content and structure of the template are known at a glance.

3. you only need one smarty instance to complete all the tasks and reduce the usage of system resources.

Disadvantages:

1. variables in the subtemplate may conflict with those in the parent template.

2. variable name conflicts increase in the case of multiple nesting, making it more difficult to assign values to all variables.

3. the sub-template has poor controllability. for example, you cannot set $ cache_id or $ compile_id to obtain multiple results.

4. poor scalability. To add a new template, you must modify the template and the program.

Method 2: Use $ smarty-> fetch () to convert the sub-template data to the parent template as the assign variable.

Advantages:

1. it is also conducive to module division and template reuse.

2. the template structure is also clear

3. the sub-template is used as a separate object and is highly operable.

4. good scalability

5. each sub-template is a separate object and no variable name conflict occurs.

Disadvantages:

1. if there are many sub-templates, multiple smarty instances are required to implement them, and the PHP code looks bloated.

2. occupying a large amount of system resources

3 ,......

At that time, out of performance requirements and the absence of tests, it is assumed that the performance of include must be higher than that of fetch, with the increasing number of system functions, I feel that writing is more complicated, which is mainly caused by design problems, however, nesting between templates is also one of the problems, so we will look back and consider using the fetch method. We tested the performance of this method before making another decision. we tested the performance of a single template and multiple templates respectively.

Test a single sub-template:

There are three pages: test1.php, test2.php, and test3.php. All three pages output the same content. The template of test1.php is not nested. the template of test2.php uses an include, and test3.php uses fetch to implement nesting of subtemplates. Average time required to access each page using the AB test provided by apache (unit: ms)

During the first Test, AB-n 10000 was used to perform 10 operations on the preceding three pages. a total of 10000 accesses were made each time. The results are as follows.

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
Total 47.719 53.647 54.243 1.124227247 1.136717031

In the second Test, AB-n 100000 is used to perform one operation on the preceding three pages. The result is as follows.

AB-n 100000 Test1.php Test2.php Test3.php Test2/test1 Test3/test1
1 5.723 7.874 8.55 1.375851826 1.493971693

In the data tested for the first time, the fluctuations in test1 and test2 are obvious, while test3 is relatively stable. this phenomenon makes me very strange. From the average data point of view, the test results are still as expected. test1 has the best performance. test2 consumes 12.42% more time than test1, and test3 consumes 13.67% more time than test1, however, the difference between test2 and test3 is not big, but only about 1.3%.

In the results of the second Test, the execution time of the three pages is later than. 37: 1.49. The strange thing is that this time is fluctuating...

Test multiple sub-templates:

There are also three pages: test1.php, test2.php, and test3.php. All three pages output the same content. The template of test1.php is not nested. the template of test2.php uses 10 include embedded 10 sub-templates, and test3.php uses 10 fetch embedded 10 sub-templates. Average time required to access each page using the AB test provided by apache (unit: ms)

Because of the time, I only tested AB-n 50000 once. The data is as follows:

AB-n 50000 Test1.php Test2.php Test3.php Test2/test1 Test3/test1
1 5.68 6.054 6.028 1.06584507 1.061267606

Although the testing methods and tools may not be very good, I think you should have a thorough understanding of the methods to be used in actual applications through the comparison of these data.

I hope you can discuss more about the shortcomings and errors.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.