PHP efficiency meter: Start with quotation marks (single quotation mark, double quotation mark efficiency difference)

Source: Internet
Author: User

All along, the execution efficiency of the program is People's concern, in PHP is no exception, from the simplest quotation marks, but also thinking about his efficiency.
We have done experiments in particular, and we claim that the experiment may have been unprecedented and has not been followed.
Our experiment was based on the most elementary experimental methods: Cumulative method and multiple experiments to find the average.

experiment One: efficiency in the ordinary case
Our experiment was to execute a 10,000-byte file 5,000 times to get the speed of the parsing process.
First of all, this experiment specifically stipulates a unit, which is to better represent and eliminate the differences in the efficiency of the computer itself.
Time Unit TT, which represents about 7.2x10[sup]-8[/sup]s.
We obtained four sets of experimental data, which can be understood as the time when the ' a ' is faster than "a":
0.29 TT, about 0.0000000206s
0.46 TT, about 0.0000000328s
0.38 TT, about 0.0000000275s
0.41 TT, about 0.0000000292s
Average: 0.39 TT ( ' aaaa .... (139) ' efficiency close to ' AAA ... (100) "
Conclusion: The use of single quotation marks is faster in the same amount of data and in normal cases (without escaping).

experiment Two: efficiency of single-quote escaping
Our experiment was 5,000 times the same as "single quotes, double quotes" to execute two copies of the data, but one of them is specifically used to denote single quotes.
Time Unit TT, which represents about 7.2x10[sup]-8[/sup]s.
We obtained four sets of experimental data, which can be interpreted as saying "'" faster than "the time:
0.19 TT, about 0.0000000138s
0.15 TT, about 0.0000000110s
0.23 TT, about 0.0000000162s
0.24 TT, about 0.0000000173s

Average: 0.2 TT ("' ... (12) "Efficiency is close to" ... (10) " )
Conclusion: If you need to represent single quotes (MySQL queries) in a string, you should avoid escaping as much as possible, so use double quotes.

Experiment Three: The efficiency of curly braces
Our experiment was performed 1000 times with a certain number of variables tagged files, to obtain the speed of the parsing process.
Time Unit TT, which represents about 1.581x10[sup]-7[/sup]s~1.666x10[sup]-7[/sup]s. (TT change)
Get three sets of data, which means "{$ABC}" is faster than "$ABC".
15.3 TT, about 0.0000024186s
14.5 TT, about 0.0000024093s
15.3 TT, about 0.0000024152s
The data is very alarming and the average resultTT
This means: Use curly braces for parsing speed, about 16 times times less than curly braces.
Conclusion: The variable is represented in double quotation marks, and curly braces are used whenever possible.

experiment Four: continuous and discontinuous efficiency
Our experiment was performed 1000 times with a certain number of variables tagged files, to obtain the speed of the parsing process.
Time Unit TT, which represents about 1.581x10[sup]-7[/sup]s~1.666x10[sup]-7[/sup]s. (TT change)
The first set of experimental data, indicating "{$ABC} {$ABC} {$abc} ..." than $abc. $abc. $ABC Average resolution of each variable is fast
22.45 TT, about 0.0000035498s
21.03 TT, about 0.0000035037s
22.12 TT, about 0.0000034930s
Curly braces (continuous) faster than single quotes (discontinuous), average resultTT
This means: Using curly braces, the parsing speed is approximately 23 times times the use of discontinuous single quotes.
Conclusion: in single quotesrepresenting multiple variables(or small amount of data), swap with double references as much as possible, and use curly braces.
The second set of experimental data shows that "... $abc $abc$abc ..." than ' ... $abc. $abc. $abc. ' ...‘ Average resolution of each variable is fast
7.15 TT, about 0.0000011311s
6.57 TT, about 0.0000010943s
6.83 TT, about 0.0000010777s
Double quotes (continuous) faster than single quotes (discontinuous), average result7 TT。 (This data changes)
This means: For a long time, the parsing speed using double quotation marks (without curly braces) is approximately 8 times times the use of discontinuous single quotes.
Conclusion: in double quotation marksrepresent multiple variables (or a small amount of data), you need to use the "Join operator" faster than using single quotes.
Conclusion:When multiple variables are represented(or small amount of data), it should be possible to use double quotation marks and use curly braces.
(Attached: The author speculates that this may have something to do with the allocation of memory space, which may cause memory to adjust continuously)

Final Conclusion:
First, when you represent simple data (you don't need to escape), use single quotes as much as possible.
' Cal:are good at long? '
However, if you need to escape (that is, include single quotes) because you use single quotes, consider using double quotation marks.
' Cal:yes. But, you know, it ' s written. '="Cal:yes. But, you know, it ' s written. "
If you need to represent a variable, use curly braces whenever possible.
"Cal: $to"="Cal: {$to}, you shouldn ' t go shopping."
Try to make it continuous:
' Cal: '. $calsaid="Cal: $calsaid"="Cal: {$calsaid}"


PHP efficiency meter: Start with quotation marks (single quotation mark, double quotation mark efficiency difference)

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.