note 77998 added to language.operators.increment
| From: | michal dot kocarek at NO_SPAM dot seznam dot cz | Date: | Sun, 23 Sep 2007 10:04:26 +0000 |
| Subject: | note 77998 added to language.operators.increment | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130709@lists.php.net to get a copy of this message | ||
Speed tip:
Do not use post-incrementation/post-decrementation ($i++, $i--) where you do not work with the
result of this expression.
(For novices: Yes, every expression returns an result, also $a = '5' returns result, same
as $a && $b. And this consumes more time and resources.)
When writing loops, replace the post-incrementation with pre-incrementation, it is around 3times
faster than post-incrementation.
Why? In post-incrementation, PHP needs to copy variable value somewhere, then it increments the
value, then returns the value which was stored before the incrementation was done. No matter if you
don't expect the return value, PHP is scripting language, not compiled one, so it doesn't
optimize use of return values.
<?php
// Good practice for loop:
$array_count = count($array); // Store temporarily instead of calling everytime in loop
for ($i = 0; $i < $max_count; ++$i) { // Use pre-incrementation here, it is faster
// do something here
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 83.208.62.60
----
Manual Page -- http://www.php.net/manual/en/language.operators.increment.php
Edit -- https://master.php.net/note/edit/77998
Del: integrated -- https://master.php.net/note/delete/77998/integrated
Del: useless -- https://master.php.net/note/delete/77998/useless
Del: bad code -- https://master.php.net/note/delete/77998/bad+code
Del: spam -- https://master.php.net/note/delete/77998/spam
Del: non-english -- https://master.php.net/note/delete/77998/non-english
Del: in docs -- https://master.php.net/note/delete/77998/in+docs
Del: other reasons-- https://master.php.net/note/delete/77998
Reject -- https://master.php.net/note/reject/77998
Search -- https://master.php.net/manage/user-notes.php