GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
Benchmark::Stopwatch(3) User Contributed Perl Documentation Benchmark::Stopwatch(3)

Benchmark::Stopwatch - simple timing of stages of your code.

    use Benchmark::Stopwatch;
    my $stopwatch = Benchmark::Stopwatch->new->start;

    # ... code that reads from database ...
    $stopwatch->lap('read from database');

    # ... code that writes to disk ...
    $stopwatch->lap('write to disk');

    print $stopwatch->stop->summary;

    # NAME                        TIME        CUMULATIVE      PERCENTAGE
    #  read from database          0.123       0.123           34.462%
    #  write to disk               0.234       0.357           65.530%
    #  _stop_                      0.000       0.357           0.008%

The other benchmark modules provide excellent timing for specific parts of your code. This module aims to allow you to easily time the progression of your code.

The stopwatch analogy is that at some point you get a "new" stopwatch and "start" timing. Then you note certain events using "lap". Finally you "stop" the watch and then print out a "summary".

The summary shows all the events in order, what time they occured at, how long since the last lap and the percentage of the total time. Hopefully this will give you a good idea of where your code is spending most of its time.

The times are all wallclock times in fractional seconds.

That's it.

    my $stopwatch = Benchmark::Stopwatch->new;

Creates a new stopwatch.

    $stopwatch = $stopwatch->start;

Starts the stopwatch. Returns a reference to the stopwatch so that you can chain.

    $stopwatch = $stopwatch->lap( 'name of event' );

Notes down the time at which an event occurs. This event will later appear in the summary.

    $stopwatch = $stopwatch->stop;

Stops the stopwatch. Returns a reference to the stopwatch so you can chain.

    my $time_in_seconds = $stopwatch->total_time;

Returns the time that the stopwatch ran for in fractional seconds. If the stopwatch has not been stopped yet then it returns time it has been running for.

    my $summary_text = $stopwatch->summary;

Returns text summarizing the events that occured. Example output from a script that fetches the homepages of the web's five busiest sites and times how long each took.

 NAME                        TIME        CUMULATIVE      PERCENTAGE
  http://www.yahoo.com/       3.892       3.892           22.399%
  http://www.google.com/      3.259       7.152           18.758%
  http://www.msn.com/         8.412       15.564          48.411%
  http://www.myspace.com/     0.532       16.096          3.062%
  http://www.ebay.com/        1.281       17.377          7.370%
  _stop_                      0.000       17.377          0.000%

The final entry "_stop_" is when the stop watch was stopped.

  my $data_structure_hashref = $stopwatch->as_data;

Returns a data structure that contains all the information that was logged. This is so that you can use this module to gather the data but then use your own code to manipulate it.

The returned hashref will look like this:

  {
    start_time => 1234500,  # The time the stopwatch was started
    stop_time  => 1234510,  # The time it was stopped or as_data called
    total_time => 10,       # The duration of timing
    laps       => [
        {
            name       => 'test', # The name of the lap
            time       => 1,      # The time of this lap (seconds)
            cumulative => 1,      # seconds since start to this lap
            fraction   => 0.10,   # fraction of total time.
        },
        {
            name       => '_stop_',   # created as needed
            time       => 9,
            cumulative => 10,
            fraction   => 0.9,
        },
    ],
  }

Edmund von der Burg "<evdb@ecclestoad.co.uk">

<http://www.ecclestoad.co.uk>

Inspiration from my colleagues at <http://www.nestoria.co.uk>

Copyright (C) 2006 Edmund von der Burg. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. If it breaks you get to keep both pieces.

THERE IS NO WARRANTY.

2007-09-13 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.