PEAR Articles

  Home arrow PEAR Articles arrow Page 3 - Introduction to PEAR
PEAR ARTICLES

Introduction to PEAR
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 8
    2003-09-30

    Table of Contents:
  • Introduction to PEAR
  • PEAR Installation
  • The Benchmark Package
  • Conclusion

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    advertisement

    Introduction to PEAR - The Benchmark Package


    (Page 3 of 4 )

    The Benchmark package is now installed and ready to go. The desired extensions are made available to our script by using the require_once statement to include the appropriate file.

    <?php
    require_once "Benchmark/Timer.php";
    ?>

    If PHP complains it was unable find the file, you may need to modify your php.ini file's include_path directive. Or, you can temporarily append the directory to the include path for the duration of the script by using the ini_set() function.

    <?php
    ini_set
    ("include_path"
            
    ini_get("include_path") . "/usr/local/lib/php");
    require_once 
    "Benchmark/Timer.php";
    ?>

    The Benchmark module times the execution of a script and displays the results in the form of a table.

    <?php
    require_once "Benchmark/Timer.php";

    $t = new Benchmark_Timer();
    $t-&gt;start();
    echo 
    "Hello, World! &lt;br /&gt;";
    $t-&gt;stop();

    echo 
    "&lt;pre&gt;" $t-&gt;display() . "&lt;/pre&gt;";
    ?>

    A new timer object is created with new Benchmark_Timer and is made accessible by assigning it to the variable $t. The object can then be manipulated by the methods start, stop and display. Intuitively, start will start the timer, stop will stop the timer and display will output the results table.

    The output of the above script would resemble the following:

    Hello World!
    -------------------------------------------------------------
    marker    time index            ex time               perct
    -------------------------------------------------------------
    Start     1064782761.34820600   -                       0.00%
    -------------------------------------------------------------
    Stop      1064782761.34845500   0.00024890899658203   100.00%
    -------------------------------------------------------------
    total     -                     0.00024890899658203   100.00%
    -------------------------------------------------------------

    You may even set markers throughout your code to compare the execution time of various sections. This is done with the setMarker method which accepts a string to identify the marker.

    <?php
    require_once "Benchmark/Timer.php";

    $t = new Benchmark_Timer();
    $t-&gt;start();
    echo 
    "Hello, World! &lt;br /&gt;";

    $t-&gt;setMarker("Section One");
    for (
    $count 0$count &lt100$count++) {
         echo 
    $count+" ";
    }
    echo 
    "&lt;br /&gt;";

    $t-&gt;setMarker("Section Two");
    echo 
    "Good Bye! &lt;br /&gt;";

    $t-&gt;stop();

    echo 
    "&lt;pre&gt;" $t-&gt;display() . "&lt;/pre&gt;";
    ?>

    The markers create new entries in the final results table.

    Hello, World!
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    96 97 98 99 100
    Good Bye!
    -------------------------------------------------------------------
    marker          time index            ex time               perct
    -------------------------------------------------------------------
    Start           1064783416.23436900   -                       0.00%
    -------------------------------------------------------------------
    Section One     1064783416.23462200   0.00025296211242676     6.04%
    -------------------------------------------------------------------
    Section Two     1064783416.23846400   0.0038419961929321     91.80%
    -------------------------------------------------------------------
    Stop            1064783416.23855400   9.000301361084E-05      2.15%
    -------------------------------------------------------------------
    total           -                     0.0041849613189697    100.00%
    -------------------------------------------------------------------

    The benchmarking utility provided through PEAR is easy to implement and is a useful tool to examine the efficiency of your scripts... but it's also only one of many useful PEAR packages. To date over 200 packages are available through the PEAR Package Manager and are organized in various repositories.

    More PEAR Articles Articles
    More By bluephoenix

    blog comments powered by Disqus

    PEAR ARTICLES ARTICLES

    - Installing PEAR
    - PEAR: an Introduction
    - Managing robots.txt using PHP: Generating Dy...
    - Deleting Authors from a PEAR Content Managem...
    - PEAR CMS: Index and Delete Scripts
    - Listing Articles for a PEAR Content Manageme...
    - Building an Authors Page for a PEAR CMS
    - Building the View Details Page in a PEAR CMS
    - Creating the Main Pages of a PEAR CMS
    - Completing the Login Script for a PEAR CMS
    - User Authentication for a PEAR CMS
    - A PEAR CMS: Examining the Code
    - Building a Content Management System with PE...
    - Installing a PEAR Package
    - My PEAR: The Beginning


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap