note 93271 added to language.operators.increment

From: Date: Mon, 31 Aug 2009 22:35:24 +0000
Subject: note 93271 added to language.operators.increment
Groups: php.notes 
Request: Send a blank email to php-notes+get-160095@lists.php.net to get a copy of this message
When using the ++ operator by itself on a variable, ++$var is faster than $var++ and uses slightly less memory (in my experiments). It would seem like this could be optimized in the language during runtime (if $var++ is the only thing in the whole statement, it could be treated as ++$var). I conducted many tests (I believe to be fair), and here's one of the results: $i++ took 8.47515535355 seconds and 2360 bytes ++$i took 7.80081486702 seconds and 2160 bytes Here's my code. If anyone sees a bias in it, tell me. I conducted it many times, each time going through a loop one million iterations and doing each test 10 - 15 times (10 - 15 million uses of the ++ operator). <?php ini_set( 'MAX_EXEC_TIME', 120 ); ob_start( ); $num_tests = 10; $startFirst = $startSecond = $endFirst = $endSecond = $startFirstMemory = $endFirstMemory = $startSecondMemory = $endSecondMemory = $someVal = 0; $times = array( '$i++' => array( 'time' => 0, 'memory' => 0 ), '++$i' => array( 'total' => 0, 'memory' => 0 ) ); for( $j = 0; $j < $num_tests; ++$j ) { for( $i = 0, $startFirstMemory = memory_get_usage( ), $startFirst = microtime( true ); $i < 10000000; $i++ ){ $someval = 2; } $endFirstMemory = memory_get_usage( ); $endFirst = microtime( true ); for( $i = 0, $startSecondMemory = memory_get_usage( ), $startSecond = microtime( true ); $i < 10000000; ++$i ){ $someval = 2; } $endSecondMemory = memory_get_usage( ); $endSecond = microtime( true ); $times[ '$i++' ][ $j ] = array( 'startTime' => $startFirst, 'endTime' => $endFirst, 'startMemory' => $startFirstMemory, 'endMemory' => $endFirstMemory ); $times[ '++$i' ][ $j ] = array( 'startTime' => $startSecond, 'endTime' => $endSecond, 'startMemory' => $startSecondMemory, 'endMemory' => $endSecondMemory ); } for( $i = 0; $i < $num_tests; ++$i ) { $times[ '$i++' ][ 'time' ] += ( $times[ '$i++' ][ $i ][ 'endTime' ] - $times[ '$i++' ][ $i ][ 'startTime' ] ); $times[ '++$i' ][ 'time' ] += ( $times[ '++$i' ][ $i ][ 'endTime' ] - $times[ '++$i' ][ $i ][ 'startTime' ] ); $times[ '$i++' ][ 'memory' ] += ( $times[ '$i++' ][ $i ][ 'endMemory' ] - $times[ '$i++' ][ $i ][ 'startMemory' ] ); $times[ '++$i' ][ 'memory' ] += ( $times[ '++$i' ][ $i ][ 'endMemory' ] - $times[ '++$i' ][ $i ][ 'startMemory' ] ); } echo 'There were ' . $num_tests . ' tests conducted, here\'s the totals<br /><br /> $i++ took ' . $times[ '$i++' ][ 'time' ] . ' seconds and ' . $times[ '$i++' ][ 'memory' ] . ' bytes<br /> ++$i took ' . $times[ '++$i' ][ 'time' ] . ' seconds and ' . $times[ '++$i' ][ 'memory' ] . ' bytes'; ob_end_flush( ); ?> Try it yourself, ;) ---- Server IP: 69.147.83.197 Probable Submitter: 71.136.198.195 ---- Manual Page -- http://www.php.net/manual/en/language.operators.increment.php Edit -- https://master.php.net/note/edit/93271 Del: integrated -- https://master.php.net/note/delete/93271/integrated Del: useless -- https://master.php.net/note/delete/93271/useless Del: bad code -- https://master.php.net/note/delete/93271/bad+code Del: spam -- https://master.php.net/note/delete/93271/spam Del: non-english -- https://master.php.net/note/delete/93271/non-english Del: in docs -- https://master.php.net/note/delete/93271/in+docs Del: other reasons-- https://master.php.net/note/delete/93271 Reject -- https://master.php.net/note/reject/93271 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#160095) next »