Hey, #PHP friends - what's the *easiest* way to compare the speed & memory use of two different functions?

The functions themselves are quite simple (a dozen or so lines) so I can't really be bothered to set up a whole XDebug environment.

Is there an online tool which will give me a rough answer?

Reply to this note

Please Login to reply.

Discussion

nostr:npub19lt4284mghqxekzm6n5njxurnxrxhqhrva2leusdsuu5ja5jeycq66qfjk If you want a rough and ready figure you can call microtime before and after and compare the results, likewise memory_get_usage. They aren't great but they will probably tell you if one function is taking 10x as long as another.

If they're not enough then really Xdebug + an analyser such as KCacheGrind are the way to go.

https://3v4l.org/ allows you to run code online but I don't think it would help with profiling.

You can do a simple profile of these metrics yourself like so:

For speed you can use microtime: https://www.php.net/manual/en/function.microtime.php

1. Store the microtime in a variable before calling your function.

2. Call your function

3. Store the micotime in anothee variable after the function call

4. Calculate a difference between the two variables to see how many microseconds the function took to run.

For Memory you can use memory_get_usage: https://www.php.net/manual/en/function.memory-get-usage.php

Repeat the process above but with memory_get_usage.

Depending on what your function does, you may also want to add some calls to memory_get_usage within the function itself and store the results in a global variable for later printing the results.

I hope this helps.