Ways to improve the speed of your PHP operation

Source: Internet
Author: User
This article mainly share with you how to improve the speed of PHP, a total of more than 50, I hope to help everyone.

1, if you can define the method of the class asStatic, try to define it asStaticthat it's going to ᨀ the speed will be near4times.
2,$row [' id ']the speed is$row [id]of the7times.
3,EchothanPrintfast, and useEchothe multiple parameters
(use a comma instead of a period)instead of string connections, such asecho$str1, $str 2.
4, in the implementation fordetermine the maximum number of cycles before the loop, do not calculate the maximum value once per cycle, preferably using
foreachinstead.
5, unregister those unused variables, especially large arrays, to free up memory.
6, try to avoid using__get,__set,__autoload.
7,require_once ()Costly .
8,includefile, use absolute path as much as possible because it avoidsPHPgo toinclude_pathFind Files in
, it takes less time to resolve the operating system path.
9and if you want to know that the script starts executing(that is, the server side receives the client request)the moment, using
$_server[' request_time ']Better thanTime ().
Ten, the function replaces the regular expression to accomplish the same function.
One,Str_replacefunction RatioPreg_replacefunction fast, butSTRTRthe efficiency of the function isStr_replaceof the function
four times times.
A, if a string substitution function, can accept an array or character as a parameter, and the parameter length is not too long, that
You might consider writing an additional replacement code so that each pass argument is a character instead of just one line of code
Accepts an array as a parameter for querying and replacing.
-, using the Select Branch statement(that is,switch case)better than using multipleif,else ifstatement.
-, with@the practice of masking error messages is very inefficient and extremely inefficient.
the, openApacheof themod_deflatemodule, you can ᨀ high page browsing speed.
-, database connection should be turned off when used, do not use long connection.
-, error messages are costly.
-, increment local variable in method, speed is the fastest. is almost equivalent to the speed at which local variables are called in the function.
+, incrementing a global variable is slower than incrementing a local variable2times.
-, incrementing an object property(such as:$this->prop++)is slower than incrementing a local variable3times.
+, incrementing an undefined local variable is slower than incrementing a pre-defined local variable9toTenTimes.
A, defining only a local variable and not calling it in the function will also slow down the speed(the degree is equal to the increment of one
A local variable). PHPit will probably check to see if there are global variables.
at, the method invocation appears to be independent of the number of methods defined in the class, because I(both before and after the test method)Tim
Add aTenmethod, but there is no change in performance.
-, a method in a derived class runs faster than the same method defined in the base class.
-, calling an empty function with a parameter that takes the time equivalent to executing7to8the local variable increment operator of the secondary
For A similar method call takes a time close to theThe local variable increment operation of the secondary.
-,ApacheParse aPHPthe script takes more time than parsing a staticHTMLpage Slow2toTentimes.
Use static as much as possibleHTMLpages, with fewer scripts.
-, unless the script can be cached, it will be recompiled every time it is called. Introduce a set ofPHPcaching mechanism
It is usually possible to ᨀ liters25%to100%performance to exempt the compilation overhead.
-, try to do the cache, you can usememcached. memcachedis a high-performance memory object caching system,
Can be used to speed up dynamicWebapplication to mitigate database load. To the arithmetic code(OP code)Cache is useful,
So that the script does not have to recompile for each request.
in, when you manipulate a string and need to verify that its length satisfies a certain requirement, you will of course usestrlen ()
function. This function executes quite quickly because it does not do any calculations, only returns theZvalstructure(Cthe number of built-in
Data structures that are used to storePHPvariables)the length of the known string stored in the. However, becausestrlen ()is a function,
Somewhat slower, because function calls go through many steps, such as lowercase letters(refers to the function of the name lowercase
of thePHPdoes not distinguish between the case of a function name), hash lookup, which is executed with the called function. In some situations
Conditions, you can use theisset ()tips to speed up your code execution. Calledisset ()happens to be better thanstrlen ()Fast, because
Unlike the latter,isset ()as a language structure, it means that its execution does not require function lookups and small letters
Write. In other words, you're actually not spending much overhead in the top-level code that examines the length of the string.
the, when executing a variable$iis incremented or decremented,$i + +will be better than+ + $iA little slower. This difference isPHPUnique
Does not apply to other languages, so please do not modify yourCorJavacode and expect them to change immediately.
Come on, it's useless. + + $ifaster is because it only needs3Instructions(opcodes),$i + +you need4instructions. Rear-facing
Incrementing actually produces a temporary variable, which is then incremented. And the predecessor increment is directly on the original value
Increasing. This is one of the most optimized treatments, asZendof thePHPthe optimizer did. Keep this optimization in mind
processing is a good idea because not all of the instruction optimizer will do the same optimizations and
ᨀ Provider for Internet services in large numbers without an assembly instruction optimizer(ISPs)and servers.
*, not things must be object-oriented(OOP), object-oriented tends to be expensive, and each method and object invocation
Consumes a lot of memory.
$, not all data structures are implemented with classes, and arrays are useful.
Panax Notoginseng, do not subdivide the method too much, think carefully about what code you really intend to reuse?
-, you can always break the code down into methods when you need it.
the, try to use a large number ofPHPbuilt-in functions.
+, if you have a lot of time-consuming functions in your code, you can considerCextended way to implement them.
A, evaluation and inspection(Profile)your code. The inspector will tell you what parts of the code are consuming time.
XdebugThe debugger contains an inspection program, and the evaluation test can show the bottleneck of the code in general.
the,Mod_zipcan be used asApachemodules to instantly compress your data and reduce data transfer volumes
80%.
+, when you can usefile_get_contentsAlternativefile,fopen,feof,fgetsin the case of series methods, do
For quantityfile_get_contentsbecause his efficiency is much higher!but be awarefile_get_contentsWhen you open a
URLfile when thePHPVersion Issue;
-, as little as possible to file operations, althoughPHPThe file operation efficiency is also not low;
$, optimizeSelect SQLstatements, as little as possible.Insert,UpdateOperation;
$, use as much as possiblePHPintrinsic Functions(but I was looking for aPHPA function that does not exist inside is wasted
Could have written a custom function time, experience question Ah!);
-, do not declare variables inside the loop, especially large variables: objects(It's not like it's justPHPyou need to pay attention to the question.
Question.?);
-, multidimensional arrays try not to iterate over nested assignments;
the, when you can usePHPIn case of internal string manipulation functions, do not use regular expressions;
-,foreachmore efficient, use as muchforeachReplace whileand the forLoops;
Wuyi, using single quotation marks instead of double quotation marks to reference strings;
the,"withi+=1Replacei=i+1. MeetC + +habits, high efficiency.";
-, theGlobalvariables, should be used out ofunset ()off.

Related recommendations:

Tips on how to improve PHP running speed share

How to improve PHP running speed

Ways to improve the speed of PHP operation

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.