Profiling

Discussion in 'Scripts' started by beingchinmay, Aug 10, 2016.

  1. beingchinmay

    beingchinmay ECZ Member

    Joined:
    Jan 1, 2016
    Posts:
    130
    Likes Received:
    0
    PHP does not have a built-in profiler, but there are some techniques you can use to investigate code that you think has performance issues. One technique is to call the microtime( ) function to get an accurate representation of the amount of time that elapses. You can surround the code you’re profiling with calls to microtime( ) and use the values returned by microtime( ) to calculate how long the code took.


    For instance, here’s some code you can use to find out just how long it takes to produce the phpinfo( ) output:



    Reload this page several times, and you’ll see the number fluctuate slightly. Reload it often enough, and you’ll see it fluctuate quite a lot. The danger of timing a single run of a piece of code is that you may not get a representative machine load—the server might be paging as a user starts emacs, or it may have removed the source file from its cache. The best way to get an accurate representation of the time it takes to do something is to time repeated runs and look at the average of those times.


    The Benchmark class available in PEAR makes it easy to repeatedly time sections of your script. Here is a simple example that shows how you can use it:


    Fat Cutter Powder | Asthi jivak | Fat Cutter