ublo
bogdan's (micro)blog

bogdanel » php string output speed test

11:54 pm on Feb 14, 2017 | #more | tags:

during the development of a new resource-sensitive project, i wondered what is the best PHP string output method and thus compared the following:

<?php
/*  A: */	printf ('some value %d != %d' . "\n", $c, $c+1); 
/*  B: */	vprintf ('some value %d != %d' . "\n", [ $c, $c+1 ]); 
/*  C: */	vprintf ('some value %d != %d%s', [ $c, $c+1, "\n" ]); 
/*  D: */	echo sprintf ('some value %d != %d' . "\n", $c, $c+1); 
/*  E: */	echo vsprintf ('some value %d != %d' . "\n", [ $c, $c+1 ]); 
/*  F: */	echo vsprintf ('some value %d != %d%s', [ $c, $c+1, "\n" ]);
/*  G: */	echo 'some value ' . $c . ' != ' . ($c+1) . "\n";
/*  H: */	echo "some value $c != " . ($c+1) . "\n";
/*  I: */	?>some value <?php echo $c; ?> != <?php echo $c+1; ?>
<?php ?>

each statement was run inside a for loop, for one million cycles, for a total number of five times. the results were as follows:

A B C D E F G H I
0.4260 0.5836 0.6412 0.4333 0.5896 0.6781 0.4449 0.4182 0.0361
0.4220 0.6012 0.6631 0.4302 0.6210 0.6805 0.4385 0.4100 0.0361
0.4205 0.6083 0.6674 0.4306 0.6245 0.6935 0.4379 0.4168 0.0378
0.4364 0.6098 0.6697 0.4456 0.6752 0.7455 0.4530 0.4134 0.0361
0.4299 0.5964 0.6594 0.4384 0.5793 0.6538 0.4485 0.4185 0.0365

on average, the best I is 11.37 times faster than the next, followed by H, A, D, G, B, E, C and F, which is worst, 18.90 times slower than I.

Creampie