Profiling PHP Scripts without External Libraries

Do you ever feel like your PHP scripts are running a bit slower than they should? Or maybe you’re not sure why that new feature is taking so long to implement. In either case, profiling your script can help identify the sources of any performance issues. But how do you profile a PHP script without using external libraries? In this blog post, we’ll show you some scripts to do just that.

There are a few ways to profile a PHP with external libraries. These are good in big projects where you want to track many different aspects of performance.

  • One way is to use the xdebug extension. This extension is available for most web servers and can be used to instrument your code to produce profiling data.
  • Another way is to use the Blackfire extension. This extension is also available for most web servers and can be used to profile your code in order to find bottlenecks.
  • And, you can use the Zend Monitor tool. This tool is available for download from the Zend website and can be used to profile your code in order to find bottlenecks.

However, they can be overkill for simple scripts or when you’re just trying to track down one specific issue. In these cases, it’s often easier to use some basic profiling tools that are built into PHP.

PHP has a couple of functions that allow you to profile your code. You can these methods, microtime(), hrtime(), memory_get_usage(), and memory_get_peak_usage() to profile PHP scripts’ performance.

  • microtime(): It returns the number of seconds since January 1, 1970, that have elapsed including the fraction of a second. This function is only available on operating systems that support the gettimeofday() system call.
  • hrtime(): The hrtime() function returns the system’s high-resolution time. This is the time since an arbitrary point in time that is not adjustable. The timestamp it delivers is monotonic, which means it cannot be changed.
  • memory_get_usage(): This method returns the amount of memory currently being used by PHP.
  • memory_get_peak_usage(): It returns the peak memory usage of the current PHP process.

Examples

$start = microtime(true);
// Enter your code here, enjoy!
$array = array("1" => "Debugging...",
    "emoji" => "😄 😁 😆", 
    "number" => rand(100,999),
    "version" => phpversion()
);

foreach( $array as $key => $value ){
    echo $key."\t=>\t".$value."\n";
}
$end = microtime(true);

echo sprintf("Elapsed:  %f", $end - $start).PHP_EOL;

echo sprintf("Used RAM:  %s bytes", memory_get_usage());

Results:

1	=>	Debugging...
emoji	=>	😄 😁 😆
number	=>	700
version	=>	8.1.3
Elapsed:  0.000011
Used RAM:  385232 bytes

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close