r/PHPhelp • • 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!

17 Upvotes

11 comments sorted by

9

u/ddruganov May 04 '23

turned out to be XDebug

still have no idea why it takes so long but ok

2

u/DefenestrationPraha May 05 '23

6 seconds is some notorious timeout for session establishment in XDebug (writing off the top of my head, on vacation, so sorry for not providing links). During my own development, I see N+6 all the time, unless I turn off XDebug.

1

u/ddruganov May 05 '23

Oh okay thats interesting

3

u/HolyGonzo May 04 '23

I would guess that xdebug might be configured to perform profiling. When it profiles a script, every single operation is individually recorded in measure how long it takes to run, and the results can be dumped out to a valgrind file for analysis.

Profiling a billion operations would significantly increase the run time.

1

u/kAlvaro May 05 '23

I would guess that xdebug might be configured to perform profiling.

I don't do profiling very often but I'm not sure that would explain the difference here. On the other side, the information that Xdebug provides for step debugging includes a reference to the file and line, so the debugging client can display the code to user, and if there is no file to display perhaps Xdebug doesn't even bother.

4

u/32gbsd May 04 '23

The more tools you have the slower everything will get.

1

u/allen_jb May 04 '23

That's not necessarily true. Specifically regarding PHP extensions, most will have virtually no measurable impact when enabled if they aren't actually called.

xdebug is an exception because it's a debugger - it hooks into fundamental areas of PHP to record everything that's happening and allow for breakpoints and step-by-step debugging.

2

u/crazedizzled May 04 '23

XDebug has a pretty significant overhead just by the nature of what it does.

-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

u/HolyGonzo May 04 '23

While what you said is true, the speed difference is not 6 seconds.

-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.