In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "introduction to the optimization methods of php performance". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Optimize bottlenecks in your code
Hoare once said that "premature optimization is the root of all misfortune." When you want to make your site run faster, you should do something optimized. What do you need to do before you change your code? what makes the system slow? You can optimize your php through the following instructions and other ways, either for database reasons or for network reasons! By optimizing your php code, you can try to identify your system bottlenecks.
Upgrade your php version
Your team members point out that there have been many symbolic improvements in the performance of the php engine over the years. If your web server is still running an older version, such as php3 or php4. So before you try to optimize your code, you should take a closer look at the upgrades between versions.
Click the following link to learn more:
Migrate from PHP 4 to PHP 5
Migrate from PHP 5.0.x to PHP 5.1.x
Migrate from PHP 5.1.x to PHP 5.2.x
Use cach
Cache processing is performed by using a cache module (such as Memcache) or a template system (such as Smarty). We can improve website performance by caching database results and extracting page results.
Use output buffer
When your script tries to render, php will use the memory cache to save all the data. The cache may make your page look slow because the buffer fills up all the data you want to respond to and then sends the results to the user.
Fortunately, you can make a change to force php to respond to the user before the buffer fills up, which will make your site look faster.
Avoid writing childish setters and getters
When you write php classes, you can directly manipulate object properties, which can help you save time and improve your script performance. Not setters and getters that make people feel childish and ridiculous.
Here are some examples: the dog class manipulates the name property by using setName () and getName () methods.
Class dog {public $name ='; public function setName ($name) {$this- > name = $name;} public function getName () {return $this- > name;}}
Note: setName () and getName () do nothing but store and return the name property.
$rover = new dog (); $rover- > setName ('rover'); echo $rover- > getName ()
Directly set and access name properties, performance can be improved by 100%, but also can reduce development time!
$rover = new dog (); $rover- > name = 'rover';echo $rover- > name
There is no reason not to copy variables.
Sometimes an entry-level phper, in order to make the code "clean", often reassigns a defined variable to another variable. This actually leads to double memory consumption (when changing variables), which leads to a decline in script performance.
For example, if a user inserts a variable from one 512KB into another, it will cause the memory of the 1MB to be consumed.
$description = strip_tags ($_ POST ['description']); echo $description
The above code duplicates the variable for no reason. You only need to simply output variables inline without consuming extra memory.
Echo strip_tags ($_ POST ['description'])
Avoid looping SQL operations
A common mistake is to place a SQL operation in a loop, which leads to frequent access to the database and, more importantly, to poor performance of the script. In the following example, you can reset a loop operation to a single SQL statement.
Foreach ($userList as $user) {$query = 'INSERT INTO users (first_name,last_name) VALUES ("'. $user ['first_name'].'", ". $user ['last_name'].'")'; mysql_query ($query);}
Process:
INSERT INTO users (first_name,last_name) VALUES ("John", "Doe")
By replacing this loop scheme, you can splice the data into a single database operation.
$userData = array (); foreach ($userList as $user) {$userData [] ='("'. $user ['first_name'].'", ". $user ['last_name'].')';} $query = 'INSERT INTO users (first_name,last_name) VALUES'. Implode (',', $userData); mysql_query ($query)
Process:
INSERT INTO users (first_name,last_name) VALUES ("John", "Doe"), ("Jane", "Doe")... "introduction to the methods of optimizing php performance" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.