| 1 | #!/usr/local/bin/perl -w | ||
| 2 | # ======================================================================== | ||
| 3 | # delta.pl - calculate Benchmark::Timer object call overhead | ||
| 4 | # Andrew Ho (andrew@zeuscat.com) | ||
| 5 | # | ||
| 6 | # This program contains embedded documentation in Perl POD (Plain Old | ||
| 7 | # Documentation) format. Search for the string "=head1" in this document | ||
| 8 | # to find documentation snippets, or use "perldoc" to read it; utilities | ||
| 9 | # like "pod2man" and "pod2html" can reformat as well. | ||
| 10 | # | ||
| 11 | # Copyright(c) 2000-2001 Andrew Ho. | ||
| 12 | # | ||
| 13 | # This script is free software; you can redistribute it and/or modify | ||
| 14 | # it under the same terms as Perl itself. | ||
| 15 | # | ||
| 16 | # Last modified March 29, 2001 | ||
| 17 | # ======================================================================== | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | delta.pl - calculate Benchmark::Timer object call overhead | ||
| 22 | |||
| 23 | =head1 SYNOPSIS | ||
| 24 | |||
| 25 | % ./delta.pl [n] | ||
| 26 | |||
| 27 | =head1 DESCRIPTION | ||
| 28 | |||
| 29 | This short script calculates the approximate speed overhead, on your | ||
| 30 | system, of using Benchmark::Timer to time repeated benchmark calls, | ||
| 31 | rather than using Time::HiRes and lexical temporary values directly. | ||
| 32 | By default, it does 10,000 trials of timing nothing. It benchmarks | ||
| 33 | using first Benchmark::Timer, then bare Time::HiRes calls, and reports | ||
| 34 | the average times taken to perform the no-op. | ||
| 35 | |||
| 36 | Subtract the second from the first to find the approximate overhead imposed | ||
| 37 | by using Benchmark::Timer. This is typically significant and consistent, | ||
| 38 | but pretty tiny, on the order of microseconds. The magnitude of the | ||
| 39 | overhead also falls quickly and asymptotically as the number of trials | ||
| 40 | increases from one, levelling off at anything over around 100 trials. | ||
| 41 | |||
| 42 | =head1 SEE ALSO | ||
| 43 | |||
| 44 | L<Benchmark>, L<Time::HiRes> | ||
| 45 | |||
| 46 | =head1 AUTHOR | ||
| 47 | |||
| 48 | Andrew Ho E<lt>andrew@zeuscat.comE<gt> | ||
| 49 | |||
| 50 | =head1 COPYRIGHT | ||
| 51 | |||
| 52 | Copyright(c) 2000-2001 Andrew Ho. | ||
| 53 | |||
| 54 | This library is free software; you can redistribute it and/or modify | ||
| 55 | it under the same terms as Perl itself. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | |||
| 60 | # ------------------------------------------------------------------------ | ||
| 61 | # Main loop. | ||
| 62 | |||
| 63 | use strict; | ||
| 64 | |||
| 65 | use Benchmark::Timer; | ||
| 66 | use Time::HiRes qw( gettimeofday tv_interval ); | ||
| 67 | |||
| 68 | use vars qw($N); $N = shift || 10_000; | ||
| 69 | |||
| 70 | my $t = new Benchmark::Timer; | ||
| 71 | foreach(1 .. $N) { | ||
| 72 | $t->start('noop'); | ||
| 73 | $t->stop; | ||
| 74 | } | ||
| 75 | my $bt = $t->result('noop'); | ||
| 76 | printf "Benchmark::Timer => %s\n", Benchmark::Timer::timestr($bt); | ||
| 77 | |||
| 78 | my @interval = (); | ||
| 79 | foreach(1 .. $N) { | ||
| 80 | my $before = [ gettimeofday ]; | ||
| 81 | my $elapsed = tv_interval( $before, [ gettimeofday ] ); | ||
| 82 | push @interval, $elapsed; | ||
| 83 | } | ||
| 84 | my $interval = 0; $interval += $_ foreach @interval; $interval /= @interval; | ||
| 85 | printf "Time::HiRes => %s\n", Benchmark::Timer::timestr($interval); | ||
| 86 | |||
| 87 | exit 0; | ||
| 88 | |||
| 89 | |||
| 90 | # ======================================================================== | ||
| 91 | __END__ |