r/PHPhelp • u/ddruganov • May 04 '23
Solved Why is `php -r` faster than `php file.php`?
Hi! I have this little piece of code:
$ts = microtime(true);
for($i = 0; $i < 1000000000; $i++){}
$te = microtime(true);
echo("Execution time for billion is: ".($te-$ts));
Running it like php file.php yields 8.65 secs
Running it with php -r "code here" yields 2.5 seconds
I understand that there might be difference due to disk operations for file open but it couldnt possibly be 6 seconds, right?
Has anyone got any guesses?
PHP version: PHP 8.2.5 (cli) (built: Apr 14 2023 18:20:28) (NTS)
Id google but dont really have any idea how to even compose a query for this topic
Thanks in advance!
-2
u/maxloroll May 04 '23
When running a PHP script using php file.php, the PHP interpreter needs to open and read the contents of the file before it can start executing the code. This incurs some overhead, especially for large files or when the file is located on a slow disk.
On the other hand, when running PHP code using php -r, the code is passed directly to the interpreter on the command line, so there is no file opening or reading overhead. The code is simply compiled and executed, resulting in faster execution times.
3
-1
u/maxloroll May 04 '23
Overall, using php -r can provide faster execution times for small snippets of code, but for larger scripts or applications, it is generally better to use a file-based approach.
9
u/ddruganov May 04 '23
turned out to be XDebug
still have no idea why it takes so long but ok