GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing wikitech.wikimedia.org — MediaWiki Engineering/Guides/PHP optimisation tips - Wikitech (Redirected from Performance/Guides/PHP optimisation tips) Some tips for making PHP code faster. BenchmarkingConfirm any proposed performance measure by benchmarking it. Steps documented at Measure backend performance#Benchmarking. Array separationArrays in PHP have copy-on-write semantics. When you modify an array, if the reference count is one, the modification is done in place and is fast. If the reference count is more than one, the array needs to be copied before the modification can take place. This is termed "separation" in the PHP source, since you start with one value and separate it so that there are two values. Array separation means that innocuous-looking code can be surprisingly slow. function getFirstElement($array) { return reset( $array ); } $array = range(0, 1000000); getFirstElement($array); Compared to the following: function getFirstElement($array) { return $array[0]; } $array = range(0, 1000000); getFirstElement($array); Modification of the iteration pointer by $ php maintenance/benchmarks/benchmarkEval.php \
--setup 'function getFirstElement($array) { return reset( $array ); }' \
--code '$array = range(0, 1000000); getFirstElement($array);'
Running PHP version 8.2.0
eval
count: 100
rate: 56.9/s
mean: 17.57ms
Peak memory usage: 52.01 MiB
$ php maintenance/benchmarks/benchmarkEval.php \
--setup 'function getFirstElement($array) { return $array[0]; }' \
--code '$array = range(0, 1000000); getFirstElement($array);'
Running PHP version 8.2.0
eval
count: 100
rate: 122.6/s
mean: 8.16ms
Peak memory usage: 36.00 MiB
Anything that takes an array as input and returns a modified version of the array will require O(N) time. $a = []; for ( $i = 0; $i < 1000; $i++ ) { $a = array_merge( $a, [ 1 ] ); } The above code snippet requires Some observations about built-in functions:
Constant factorsPHP code is compiled to an array of operations (an oparray). The PHP VM traverses the oparray, executing each opcode as it finds it. Some ops are faster than others.
The PHP compiler is not as smart as a C compiler because it operates under strict time constraints. Do not assume the PHP compiler is going to help you out by optimising away your slow code. With a few exceptions, what you write is what it executes. Caching and memoizationMemoization means caching the result of a function call in a way that is transparent to the caller. It is often an easy and effective way to improve performance. The optimisation operatorMost languages have an optimisation operator. The optimisation operator makes any code faster when the operator is placed in front of the code in question. // $this->slowFunction(); # $this->slowFunction(); In other words, it is faster to not do a thing than to do the thing. Engineers need to push back against expensive requirements. Product managers do not necessarily understand the cost of a requirement in terms of user observed latency or hardware cost. AbstractionWirth's law states that software becomes slower as hardware becomes faster. Increasing abstractions and features keep pace with hardware improvements so that the benefits of hardware improvements are never seen by users. In fact, latency tends to increase over time. Wirth's law is inherent in the way engineers think and operate. Engineers cannot resist the dopamine hit which comes from introducing a neat abstraction. It must be consciously and continuously fought to preserve a reasonable user experience. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |