note 102010 modified in function.sprintf by danbrown

From: Date: Sun, 23 Jan 2011 17:42:25 +0000
Subject: note 102010 modified in function.sprintf by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-176221@lists.php.net to get a copy of this message
I did some performance testing on using sprintf() and perhaps just doing some variable passing. Perhaps someone else my find this useful as well. This was the loop of code that I tested with sprintf(); <?php // OPTION 1 $repeatNum = 0; do { $response = sprintf("Hey there %s how are you doing?", common::cleanUp("Justin")); echo $response; $repeatNum++; } while ($repeatNum < 100000); /********** VS **********/ // OPTION 2 $repeatNum = 0; do { $firstName = common::cleanUp("Justin"); $response = "Hey there ". $firstName ." how are you doing"; echo $response; $repeatNum++; } while ($repeatNum < 100000); ?> My Results were surprising to myself. The cleanUp() method basically runs through a series of functions which removes extra spaces and other random things. I would have expected sprintf() to be fast than using variable passing but these were my results. /* Option 1 Results */ 100,000 Loops 5 attempts each, using sprintf() FireFox 3 response time was 7.503113(avg) seconds and memory usage was 63.41 KB. IE9 response time was 1.6439422(avg) seconds and 62.26 KB. Safari response time was 1.3566706(avg) seconds and 62.47 KB. Chrome response time was 1.4568572(avg) seconds and 62.76 KB. /* Option 2 Results */ 100,000 Loops 5 attempts each, using variable passing FireFox 3 response time was 4.9872952(avg) seconds and memory usage was 63.10 KB. IE9 response time was 1.2892588(avg) seconds and 62.17 KB. Safari response time was 1.4660842(avg) seconds and 62.38 KB. Chrome response time was 1.3231708(avg) seconds and 62.67 KB. I know it is quite minor but it shows that variable passing within firefox is on average 2.5 seconds faster. And with this increase difference I tested it minimum 20 times and received the same responses. So I was quite blow away by the difference in FF. All other browsers were very close. Hope someone else finds this useful. --was-- I did some performance testing on using sprintf() and perhaps just doing some variable passing. Perhaps someone else my find this useful as well. This was the loop of code that I tested with sprintf(); // OPTION 1 $repeatNum = 0; do { $response = sprintf("Hey there %s how are you doing?", common::cleanUp("Justin")); echo $response; $repeatNum++; } while ($repeatNum < 100000); /********** VS **********/ // OPTION 2 $repeatNum = 0; do { $firstName = common::cleanUp("Justin"); $response = "Hey there ". $firstName ." how are you doing"; echo $response; $repeatNum++; } while ($repeatNum < 100000); My Results were surprising to myself. The cleanUp() method basically runs through a series of functions which removes extra spaces and other random things. I would have expected sprintf() to be fast than using variable passing but these were my results. /* Option 1 Results */ 100,000 Loops 5 attempts each, using sprintf() FireFox 3 response time was 7.503113(avg) seconds and memory usage was 63.41 KB. IE9 response time was 1.6439422(avg) seconds and 62.26 KB. Safari response time was 1.3566706(avg) seconds and 62.47 KB. Chrome response time was 1.4568572(avg) seconds and 62.76 KB. /* Option 2 Results */ 100,000 Loops 5 attempts each, using variable passing FireFox 3 response time was 4.9872952(avg) seconds and memory usage was 63.10 KB. IE9 response time was 1.2892588(avg) seconds and 62.17 KB. Safari response time was 1.4660842(avg) seconds and 62.38 KB. Chrome response time was 1.3231708(avg) seconds and 62.67 KB. I know it is quite minor but it shows that variable passing within firefox is on average 2.5 seconds faster. And with this increase difference I tested it minimum 20 times and received the same responses. So I was quite blow away by the difference in FF. All other browsers were very close. Hope someone else finds this useful. http://php.net/manual/en/function.sprintf.php

« previous php.notes (#176221) next »