File Coverage

blib/lib/Devel/TimeThis.pm
Criterion Covered Total %
statement 20 32 62.5
branch 1 2 50.0
condition n/a
subroutine 7 9 77.7
pod 1 1 100.0
total 29 44 65.9


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Devel::TimeThis - Time the duration of a variable until it goes out of
4             scope
5              
6              
7             =head1 DESCRIPTION
8              
9              
10              
11             =head1 SYNOPSIS
12              
13              
14              
15             =cut
16              
17              
18              
19              
20              
21 68     68   882 use strict;
  68         92  
  68         1664  
22 68     68   221 use warnings;
  68         71  
  68         1640  
23 68     68   218 use utf8;
  68         72  
  68         370  
24              
25             package Devel::TimeThis;
26             $Devel::TimeThis::VERSION = '0.0218';
27              
28              
29              
30              
31 68     68   2312 use Carp;
  68         91  
  68         3533  
32 68     68   247 use Data::Dumper;
  68         72  
  68         2571  
33 68     68   29109 use Time::HiRes qw/time/;
  68         66751  
  68         234  
34              
35              
36              
37              
38             =head1 PROPERTIES
39              
40             =head2 timeStart
41              
42              
43             =cut
44              
45              
46              
47              
48              
49              
50             my $rhNameInfo = {};
51              
52              
53              
54              
55             =head1 API METHODS
56              
57             =head2 new($name)
58              
59             Create new TimeThis object.
60              
61             Invocations with the same $name will be reported together.
62              
63             =cut
64             sub new() {
65 0     0 1   my $self = bless {}, shift;
66 0           my ($name) = @_;
67              
68 0           $self->{timeStart} = time();
69 0           $self->{name} = $name;
70              
71 0           return($self);
72             }
73              
74              
75              
76              
77              
78             =head2 DESTROY
79              
80             Collect the timing data
81              
82             =cut
83             sub DESTROY {
84 0     0     my ($self) = @_;
85              
86 0           my $timeDuration = time() - $self->{timeStart};
87              
88 0           $rhNameInfo->{$self->{name}}->{timeDurationAcc} += $timeDuration;
89 0           $rhNameInfo->{$self->{name}}->{count}++;
90 0           $rhNameInfo->{$self->{name}}->{name} = $self->{name};
91             }
92              
93              
94              
95              
96              
97             =head2 END
98              
99             Print timing data
100              
101             =cut
102             sub END {
103 68 50   68   396087 keys %$rhNameInfo and print qq{
104              
105             * Timing info *
106              
107             };
108 68         589 for my $rhInfo (
109 0           sort { $b->{timeDurationAcc} <=> $a->{timeDurationAcc} }
110             values %$rhNameInfo)
111             {
112 0           printf("% 40s: % 4d : %3.5f\n", $rhInfo->{name}, $rhInfo->{count}, $rhInfo->{timeDurationAcc});
113             }
114             }
115              
116              
117              
118              
119              
120             1;
121              
122              
123              
124              
125              
126             __END__
127              
128              
129             =encoding utf8
130              
131             =head1 AUTHOR
132              
133             Johan Lindstrom, C<< <johanl@cpan.org> >>
134              
135             =head1 BUGS
136              
137             Please report any bugs or feature requests to
138             C<bug-devel-perlysense@rt.cpan.org>, or through the web interface at
139             L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Devel-PerlySense>.
140             I will be notified, and then you'll automatically be notified of progress on
141             your bug as I make changes.
142              
143              
144             =head1 COPYRIGHT & LICENSE
145              
146             Copyright 2005 Johan Lindstrom, All Rights Reserved.
147              
148             This program is free software; you can redistribute it and/or modify it
149             under the same terms as Perl itself.
150              
151             =cut