NAME

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

SYNOPSIS

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%

DESCRIPTION

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.

METHODS

new

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

Creates a new stopwatch.

start

$stopwatch = $stopwatch->start;

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

lap

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

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

stop

$stopwatch = $stopwatch->stop;

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

total_time

my $time_in_seconds = $stopwatch->total_time;

Returns the time that the stopwatch ran for in fractional seconds.

summary

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.

AUTHOR

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

http://www.ecclestoad.co.uk

COPYRIGHT

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.