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"); } ?>
Method:
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;To consider only the output line
sys
of thetime
command, ‘cause only the CPU time was considered on this test;To order the time of execution of each script, from the lowest to the highest;
Throw out the first and last 25 results of execution time of each script;
To plot the remaining sample results and trace the average line of execution for each command.
The result? Here is it:
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: