note 102184 added to function.sprintf

From: Date: Tue, 01 Feb 2011 14:23:42 +0000
Subject: note 102184 added to function.sprintf
Groups: php.notes 
Request: Send a blank email to php-notes+get-176561@lists.php.net to get a copy of this message
@Justin The relative differences in your data (time on option1 divided by time on option2) differ wildly from browser to browser, plus measuring it in the browser takes into account all kinds of external factors. Something like this might give a better implression of the performance difference between sprintf() and concatenation: <?php $start = microtime(true); for($i = 0; $i < 100000; $i++) { $response = sprintf("Hey there %s how are you doing?", ("Justin")); } echo "[" . (microtime(true) - $start) . "]"; $start = microtime(true); for($i = 0; $i < 100000; $i++) { $firstName = ("Justin"); $response = "Hey there ". $firstName ." how are you doing"; } echo "[" . (microtime(true) - $start) . "]"; ?> This shows concatenation to be about 2 times faster than sprintf() for this particular case, which is probably to be expected since no parsing for %s in the format needs to be done and there's no function to be called. Predictably sprintf() also seems to perform increasingly worse the longer the format string becomes. Concatenation obviously takes longer on longer strings as well, but performance doesn't seem to degrade as rapidly. I don't know if these results mean anything. The main benefits of sprintf() are the formatting and convenient token replacement, so I guess the take-away would be not to abuse sprintf() for scenarios where you really just need concatenation. ---- Server IP: 69.147.83.197 Probable Submitter: 77.161.72.147 ---- Manual Page -- http://www.php.net/manual/en/function.sprintf.php Edit -- https://master.php.net/note/edit/102184 Del: integrated -- https://master.php.net/note/delete/102184/integrated Del: useless -- https://master.php.net/note/delete/102184/useless Del: bad code -- https://master.php.net/note/delete/102184/bad+code Del: spam -- https://master.php.net/note/delete/102184/spam Del: non-english -- https://master.php.net/note/delete/102184/non-english Del: in docs -- https://master.php.net/note/delete/102184/in+docs Del: other reasons-- https://master.php.net/note/delete/102184 Reject -- https://master.php.net/note/reject/102184 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#176561) next »