| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Misc::Stopwatch; | 
| 2 | 1 |  |  | 1 |  | 60426 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 66 |  | 
| 3 |  |  |  |  |  |  | our $VERSION = 0.1; | 
| 4 | 1 |  |  | 1 |  | 980 | use Time::HiRes qw(gettimeofday tv_interval); | 
|  | 1 |  |  |  |  | 2285 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 7 |  |  |  |  |  |  | # new - Construct a new Misc::Stopwatch | 
| 8 |  |  |  |  |  |  | # new | 
| 9 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 10 |  |  |  |  |  |  | #|test(!abort) use Misc::Stopwatch; | 
| 11 |  |  |  |  |  |  | #|test(true) my $sw = Misc::Stopwatch->new(); | 
| 12 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub new { | 
| 15 | 8 |  | 33 | 8 | 1 | 2351 | bless [], (ref($_[0]) || $_[0]); | 
| 16 |  |  |  |  |  |  | } | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 19 |  |  |  |  |  |  | # start - Reset and Start the stopwatch | 
| 20 |  |  |  |  |  |  | # start | 
| 21 |  |  |  |  |  |  | # Returns C<$self>. | 
| 22 |  |  |  |  |  |  | # | 
| 23 |  |  |  |  |  |  | # Note that calling this on an already-running instance will reset it. | 
| 24 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 25 |  |  |  |  |  |  | #|test(!abort) my $sw = Misc::Stopwatch->new()->start(); | 
| 26 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub start { | 
| 29 | 5 |  |  | 5 | 1 | 15 | $_[0]->reset(); | 
| 30 | 5 |  |  |  |  | 13 | $_[0]->_capture(); | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 34 |  |  |  |  |  |  | # lap - Preserve the elapsed time without stopping | 
| 35 |  |  |  |  |  |  | # lap | 
| 36 |  |  |  |  |  |  | # | 
| 37 |  |  |  |  |  |  | # Returns C<$self>. | 
| 38 |  |  |  |  |  |  | # | 
| 39 |  |  |  |  |  |  | # This is is a no-op unless the stopwatch is running. | 
| 40 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 41 |  |  |  |  |  |  | #|test(!abort) my $sw = Misc::Stopwatch->new()->start()->lap(); | 
| 42 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | sub lap { | 
| 45 | 2 | 50 |  | 2 | 1 | 6 | return $_[0] unless $_[0]->is_running; | 
| 46 | 2 |  |  |  |  | 7 | $_[0]->_capture(); | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 50 |  |  |  |  |  |  | # stop - Stop | 
| 51 |  |  |  |  |  |  | # stop | 
| 52 |  |  |  |  |  |  | # Returns C<$self>. | 
| 53 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 54 |  |  |  |  |  |  | #|test(!abort) my $sw = Misc::Stopwatch->new()->start()->stop(); | 
| 55 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | sub stop { | 
| 58 | 1 |  |  | 1 | 1 | 4 | $_[0]->lap(); | 
| 59 | 1 |  |  |  |  | 2 | push @{$_[0]}, undef; | 
|  | 1 |  |  |  |  | 21 |  | 
| 60 | 1 |  |  |  |  | 4 | $_[0]; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 64 |  |  |  |  |  |  | # elapsed - Return the elapsed time | 
| 65 |  |  |  |  |  |  | # elapsed $lap | 
| 66 |  |  |  |  |  |  | # elapsed | 
| 67 |  |  |  |  |  |  | # In its second form, elapsed will return the time from L to now (or when | 
| 68 |  |  |  |  |  |  | # L was called). | 
| 69 |  |  |  |  |  |  | # | 
| 70 |  |  |  |  |  |  | # C is returned when: | 
| 71 |  |  |  |  |  |  | # | 
| 72 |  |  |  |  |  |  | # 1.) C<$lap> is provided but no such lap exists | 
| 73 |  |  |  |  |  |  | # | 
| 74 |  |  |  |  |  |  | # 2.) L returns a false value | 
| 75 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 76 |  |  |  |  |  |  | #|test(true) Misc::Stopwatch->new()->start()->elapsed(); | 
| 77 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub elapsed { | 
| 80 | 1 |  |  | 1 | 1 | 3 | my ($self, $lap) = @_; | 
| 81 | 1 |  |  |  |  | 2 | my ($b, $e) = ($$self[0], undef); | 
| 82 | 1 | 50 |  |  |  | 6 | if (defined $lap) { | 
|  |  | 50 |  |  |  |  |  | 
| 83 | 0 |  |  |  |  | 0 | $e = $$self[$lap]; | 
| 84 |  |  |  |  |  |  | } elsif ($self->is_running) { | 
| 85 | 1 |  |  |  |  | 4 | $e = [gettimeofday]; | 
| 86 |  |  |  |  |  |  | } else { | 
| 87 | 0 |  |  |  |  | 0 | $e = $$self[-2]; | 
| 88 |  |  |  |  |  |  | } | 
| 89 | 1 | 50 | 33 |  |  | 13 | defined ($b && $e) ? tv_interval($b, $e) : 0; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 93 |  |  |  |  |  |  | # reset - Stop and clear data | 
| 94 |  |  |  |  |  |  | # reset | 
| 95 |  |  |  |  |  |  | # Returns C<$self>. | 
| 96 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 97 |  |  |  |  |  |  | #|test(!abort) my $sw = Misc::Stopwatch->new()->reset(); | 
| 98 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | sub reset { | 
| 101 | 6 |  |  | 6 | 1 | 7 | @{$_[0]} = (); | 
|  | 6 |  |  |  |  | 13 |  | 
| 102 | 6 |  |  |  |  | 9 | $_[0]; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 106 |  |  |  |  |  |  | # is_running - Boolean logic | 
| 107 |  |  |  |  |  |  | # is_running | 
| 108 |  |  |  |  |  |  | # Returns a true value if the stopwatch has been started and has not been | 
| 109 |  |  |  |  |  |  | # stopped. | 
| 110 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 111 |  |  |  |  |  |  | #|test(false) Misc::Stopwatch->new()->is_running(); | 
| 112 |  |  |  |  |  |  | #|test(true) Misc::Stopwatch->new()->start()->is_running(); | 
| 113 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | sub is_running { | 
| 116 | 5 |  |  | 5 | 1 | 8 | defined $_[0][$#{$_[0]}]; | 
|  | 5 |  |  |  |  | 17 |  | 
| 117 |  |  |  |  |  |  | } | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 120 |  |  |  |  |  |  | # _capture - Capture the moment | 
| 121 |  |  |  |  |  |  | # _capture | 
| 122 |  |  |  |  |  |  | # ------------------------------------------------------------------------------ | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | sub _capture { | 
| 125 | 7 |  |  | 7 |  | 7 | push @{$_[0]}, [gettimeofday]; | 
|  | 7 |  |  |  |  | 41 |  | 
| 126 | 7 |  |  |  |  | 25 | $_[0]; | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | 1; | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | __END__ |