note 77998 deleted from language.operators.increment by hholzgra

From: Date: Thu, 02 Aug 2012 15:22:59 +0000
Subject: note 77998 deleted from language.operators.increment by hholzgra
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189856@lists.php.net to get a copy of this message
Note Submitter: michal dot kocarek at NO_SPAM dot seznam dot cz ---- 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 } ?>

« previous php.notes (#189856) next »