Echo, print, printf: Who's the fastest?

Você pode ler este post em português também.

Who’s the fastest at CPU? Echo, Print or printf? Sample material:

  • Machine: HP Pavilion dv6058cl Notebook;

  • Operation system: ArchLinux, x86-64 architecture;

  • PHP version 5.3.0

  • Environment: Bash, without X. XFS filesystem.

  • File echo.php: <?php for ($i = 0; $i < 100000; $i++) { echo "This is a simple test\n"; } ?>

  • File print.php: <?php for ($i = 0; $i < 100000; $i++) { print "This is a simple test\n"; } ?>

  • File printf.php: <?php for ($i = 0; $i < 100000; $i++) { printf("This is a simple test\n"); } ?>


  1. Run each of the PHP script 100 times, and store the execution time of each one: for i in `seq 1 100`; do (time php echo.php > o_echo.txt) 2>> t_echo.txt; done; for i in `seq 1 100`; do (time php print.php > o_print.txt) 2>> t_print.txt; done; for i in `seq 1 100`; do (time php printf.php > o_printf.txt) 2>> t_printf.txt; done; Note: Between each test, the PC was turned off and turned on again after 30 seconds;

  2. To consider only the output line sys of the time command, ‘cause only the CPU time was considered on this test;

  3. To order the time of execution of each script, from the lowest to the highest;

  4. Throw out the first and last 25 results of execution time of each script;

  5. To plot the remaining sample results and trace the average line of execution for each command.

The result? Here is it: Gráfico resultante

Conclusion: The test shows that the print command is the fastest when you need to output text. The time to redirect and store the output of each file were counted as the execution of the script (possible failure). Future test ideas:

  • Long strings (output of a NOWDOC or HEREDOC variable);

  • To interpolate numeric and strings variables together.

Published At (Updated At)
Tagged with