PHP High Performance writing

Source: Internet
Author: User
From. NET to do PHP4 years, recently began to pursue high-performance ~ ~
So start to think it's time to write a blog ~
Come on, find something first ~
Copy CodeThe code is as follows:
$arr = Array (
' Attr1 ' => 1,
' Attr2 ' => 1,
' Attr3 ' => 1,
);
$startTime = Microtime (true);
for ($i = 0; $i < 1000; $i + +)
{
if (Isset ($arr [' attr1 '])
{

}
if (Isset ($arr [' attr2 '])
{

}
if (Isset ($arr [' ATTR3 '])
{

}
}
$endTime = Microtime (true);
printf ("%d us.\n", ($endTime-$startTime) * 1000000);
$startTime = Microtime (true);
for ($i = 0; $i < 1000; $i + +)
{
foreach ($arr as $key => $value)
{
Switch ($key)
{
Case ' ATTR1 ':
Break
Case ' ATTR2 ':
Break
Case ' ATTR3 ':
Break
}
}
}
$endTime = Microtime (true);
printf ("%d us.\n", ($endTime-$startTime) * 1000000);

A section of code above
The output result is
us.
us.
However, the first paragraph is more cumbersome than the second, and the structure does not have the second paragraph clear,
So why does the first paragraph run so much faster than the second paragraph?
We can see that in the first paragraph of the code, there are only 3 if,
So how many are there in the second paragraph?
We opened the switch and looked at his basic principle of implementation.
If the switch is in each case, use the break;
In fact, this switch is like multiple If{}else if{}

So from this mechanism, we can put the
Copy CodeThe code is as follows:
foreach ($arr as $key => $value)
{
Switch ($key)
{
Case ' ATTR1 ':
Break
Case ' ATTR2 ':
Break
Case ' ATTR3 ':
Break
}
}

Converted Into
Copy CodeThe code is as follows:
foreach ($arr as $key => $value)
{
if ($key = = ' attr1 ')
{

}
else if ($key = = ' attr2 ')
{

}
else if ($key = = ' Attr3 ')
{

}
}


To understand,
From here you can see that the second code will be based on the number of keys in the array to constantly judge the number of times as 1+2+3 judgment, so the first code to judge the number of times is 3, and the second code to judge the number of times is 6 times


So it leads to a nearly one-fold rate of execution efficiency.

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.