| 1 | # ----------------------------------------------------------------------------- | ||||||||||||
| 2 | |||||||||||||
| 3 | =encoding utf8 | ||||||||||||
| 4 | |||||||||||||
| 5 | =head1 NAME | ||||||||||||
| 6 | |||||||||||||
| 7 | Quiq::Stopwatch - Zeitmesser | ||||||||||||
| 8 | |||||||||||||
| 9 | =head1 BASE CLASS | ||||||||||||
| 10 | |||||||||||||
| 11 | L<Quiq::Object> | ||||||||||||
| 12 | |||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||
| 14 | |||||||||||||
| 15 | Eine Stoppuhr: | ||||||||||||
| 16 | |||||||||||||
| 17 | use Quiq::Stopwatch; | ||||||||||||
| 18 | |||||||||||||
| 19 | my $stw = Quiq::Stopwatch->new; | ||||||||||||
| 20 | ... | ||||||||||||
| 21 | printf "%.2f\n",$stw->elapsed; | ||||||||||||
| 22 | |||||||||||||
| 23 | Zwei Stoppuhren für Gesamtzeit und Zwischenzeiten: | ||||||||||||
| 24 | |||||||||||||
| 25 | use Quiq::Stopwatch; | ||||||||||||
| 26 | |||||||||||||
| 27 | my $stw = Quiq::Stopwatch->new(2); # Start von zwei Timern | ||||||||||||
| 28 | ... | ||||||||||||
| 29 | printf "%.2f\n",$stw->restart(1); # Abschnittszeit (Timer 1) | ||||||||||||
| 30 | ... | ||||||||||||
| 31 | printf "%.2f\n",$stw->restart(1); # Abschnittszeit (Timer 1) | ||||||||||||
| 32 | ... | ||||||||||||
| 33 | printf "%.2f\n",$stw->elapsed; # Gesamtzeit (Timer 0) | ||||||||||||
| 34 | |||||||||||||
| 35 | =head1 DESCRIPTION | ||||||||||||
| 36 | |||||||||||||
| 37 | Die Klasse implementiert einen einfachen hochauflösenden Zeitmesser. | ||||||||||||
| 38 | Mit Aufruf des Konstruktors wird die Zeitmessung gestartet. Mit der | ||||||||||||
| 39 | Methode elapsed() kann die seitdem vergangene Zeit abgefragt werden. | ||||||||||||
| 40 | Mit der Methode start() wird der Zeitmesser neu gestartet und | ||||||||||||
| 41 | die seit dem letzten Start vergangene Zeit zurückgeliefert. | ||||||||||||
| 42 | Mittels letzterer Methode ist es möglich, einzelne Codeabschnitte | ||||||||||||
| 43 | zu messen, ohne einen neuen Zeitmesser instantiieren zu müssen. | ||||||||||||
| 44 | Die Zeit wird in Sekunden gemessen. Die Genauigkeit (d.h. die maximale | ||||||||||||
| 45 | Anzahl der Nachkommastellen) ist systemabhängig. | ||||||||||||
| 46 | Es können $n Zeitmessungen gleichzeitig geführt werden. | ||||||||||||
| 47 | |||||||||||||
| 48 | =head1 SEE ALSO | ||||||||||||
| 49 | |||||||||||||
| 50 | Klasse Quiq::Duration | ||||||||||||
| 51 | |||||||||||||
| 52 | =cut | ||||||||||||
| 53 | |||||||||||||
| 54 | # ----------------------------------------------------------------------------- | ||||||||||||
| 55 | |||||||||||||
| 56 | package Quiq::Stopwatch; | ||||||||||||
| 57 | 3 | ▶ | use base qw/Quiq::Object/; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 58 | |||||||||||||
| 59 | 3 | ▶ | use v5.10; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 60 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 61 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 62 | |||||||||||||
| 63 | 3 | our $VERSION = '1.240'; | |||||||||||
| 64 | |||||||||||||
| 65 | 3 | ▶ | use Time::HiRes (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 66 | 3 | ▶ | use Quiq::Duration; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 67 | |||||||||||||
| 68 | # ----------------------------------------------------------------------------- | ||||||||||||
| 69 | |||||||||||||
| 70 | =head1 METHODS | ||||||||||||
| 71 | |||||||||||||
| 72 | =head2 Konstruktor | ||||||||||||
| 73 | |||||||||||||
| 74 | =head3 new() - Instantiiere Objekt | ||||||||||||
| 75 | |||||||||||||
| 76 | =head4 Synopsis | ||||||||||||
| 77 | |||||||||||||
| 78 | $stw = $class->new; | ||||||||||||
| 79 | $stw = $class->new($n); | ||||||||||||
| 80 | |||||||||||||
| 81 | =head4 Arguments | ||||||||||||
| 82 | |||||||||||||
| 83 | =over 4 | ||||||||||||
| 84 | |||||||||||||
| 85 | =item $n (Default: 1) | ||||||||||||
| 86 | |||||||||||||
| 87 | Anzahl der Timer. Diese werden mit 0 .. $n-1 bezeichnet. | ||||||||||||
| 88 | |||||||||||||
| 89 | =back | ||||||||||||
| 90 | |||||||||||||
| 91 | =head4 Returns | ||||||||||||
| 92 | |||||||||||||
| 93 | Stopwatch-Objekt | ||||||||||||
| 94 | |||||||||||||
| 95 | =head4 Description | ||||||||||||
| 96 | |||||||||||||
| 97 | Instantiiere eine Stopwatch mit $n Timern und setze sie auf den | ||||||||||||
| 98 | aktuellen Zeitpunkt. | ||||||||||||
| 99 | |||||||||||||
| 100 | =cut | ||||||||||||
| 101 | |||||||||||||
| 102 | # ----------------------------------------------------------------------------- | ||||||||||||
| 103 | |||||||||||||
| 104 | 1 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 105 | 1 | my $class = shift; | |||||||||||
| 106 | 1 | ▶ | my $n = shift // 1; | ||||||||||
|
Condition50%1/2
shift() // 1
MC/DC0%0/1
shift() // 1
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 107 | |||||||||||||
| 108 | 1 | my $t0 = Time::HiRes::gettimeofday; | |||||||||||
| 109 | |||||||||||||
| 110 | 1 | return bless [($t0) x $n],$class; | |||||||||||
| 111 | } | ||||||||||||
| 112 | |||||||||||||
| 113 | # ----------------------------------------------------------------------------- | ||||||||||||
| 114 | |||||||||||||
| 115 | =head2 Objektmethoden | ||||||||||||
| 116 | |||||||||||||
| 117 | =head3 elapsed() - Vergangene Zeit in Sekunden | ||||||||||||
| 118 | |||||||||||||
| 119 | =head4 Synopsis | ||||||||||||
| 120 | |||||||||||||
| 121 | $sec = $stw->elapsed; | ||||||||||||
| 122 | $sec = $stw->elapsed($i); | ||||||||||||
| 123 | |||||||||||||
| 124 | =head4 Arguments | ||||||||||||
| 125 | |||||||||||||
| 126 | =over 4 | ||||||||||||
| 127 | |||||||||||||
| 128 | =item $i (Default: 0) | ||||||||||||
| 129 | |||||||||||||
| 130 | Index des abgefragten Timers. | ||||||||||||
| 131 | |||||||||||||
| 132 | =back | ||||||||||||
| 133 | |||||||||||||
| 134 | =head4 Returns | ||||||||||||
| 135 | |||||||||||||
| 136 | Sekunden (Float) | ||||||||||||
| 137 | |||||||||||||
| 138 | =head4 Description | ||||||||||||
| 139 | |||||||||||||
| 140 | Liefere die Zeit, die auf Timer $i seit dem (letzten) Start vergangen ist. | ||||||||||||
| 141 | |||||||||||||
| 142 | =cut | ||||||||||||
| 143 | |||||||||||||
| 144 | # ----------------------------------------------------------------------------- | ||||||||||||
| 145 | |||||||||||||
| 146 | 1 | ▶ | sub elapsed { | ||||||||||
|
Subroutine
elapsed: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||
| 147 | 1 | my $self = shift; | |||||||||||
| 148 | 1 | ▶ | my $i = shift // 0; | ||||||||||
|
Condition50%1/2
shift() // 0
MC/DC0%0/1
shift() // 0
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||
| 149 | 1 | return Time::HiRes::gettimeofday-$self->[$i]; | |||||||||||
| 150 | } | ||||||||||||
| 151 | |||||||||||||
| 152 | # ----------------------------------------------------------------------------- | ||||||||||||
| 153 | |||||||||||||
| 154 | =head3 elapsedReadable() - Vergangene Zeit in lesbarer Darstellung | ||||||||||||
| 155 | |||||||||||||
| 156 | =head4 Synopsis | ||||||||||||
| 157 | |||||||||||||
| 158 | $duration = $stw->elapsedReadable; | ||||||||||||
| 159 | $duration = $stw->elapsedReadable($i); | ||||||||||||
| 160 | |||||||||||||
| 161 | =head4 Arguments | ||||||||||||
| 162 | |||||||||||||
| 163 | =over 4 | ||||||||||||
| 164 | |||||||||||||
| 165 | =item $i (Default: 0) | ||||||||||||
| 166 | |||||||||||||
| 167 | Index des abgefragten Timers. | ||||||||||||
| 168 | |||||||||||||
| 169 | =back | ||||||||||||
| 170 | |||||||||||||
| 171 | =head4 Returns | ||||||||||||
| 172 | |||||||||||||
| 173 | Dauer (String) | ||||||||||||
| 174 | |||||||||||||
| 175 | =head4 Description | ||||||||||||
| 176 | |||||||||||||
| 177 | Liefere die Zeit, die auf Timer $i seit dem (letzten) Start vergangen ist, | ||||||||||||
| 178 | in der lesbaren Darstellung DdHhMmSs. | ||||||||||||
| 179 | |||||||||||||
| 180 | =cut | ||||||||||||
| 181 | |||||||||||||
| 182 | # ----------------------------------------------------------------------------- | ||||||||||||
| 183 | |||||||||||||
| 184 | 0 | ▶ | sub elapsedReadable { | ||||||||||
|
Subroutine
elapsedReadable: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 185 | 0 | my $self = shift; | |||||||||||
| 186 | 0 | ▶ | my $i = shift // 0; | ||||||||||
|
Condition0%0/2
shift() // 0
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 187 | |||||||||||||
| 188 | 0 | my $sec = Time::HiRes::gettimeofday-$self->[$i]; | |||||||||||
| 189 | 0 | return Quiq::Duration->new($sec)->asString; | |||||||||||
| 190 | } | ||||||||||||
| 191 | |||||||||||||
| 192 | # ----------------------------------------------------------------------------- | ||||||||||||
| 193 | |||||||||||||
| 194 | =head3 restart() - Starte Timer neu und liefere vergangene Zeit | ||||||||||||
| 195 | |||||||||||||
| 196 | =head4 Synopsis | ||||||||||||
| 197 | |||||||||||||
| 198 | $sec = $stw->restart; | ||||||||||||
| 199 | $sec = $stw->restart($i); | ||||||||||||
| 200 | |||||||||||||
| 201 | =head4 Arguments | ||||||||||||
| 202 | |||||||||||||
| 203 | =over 4 | ||||||||||||
| 204 | |||||||||||||
| 205 | =item $i (Default: 0) | ||||||||||||
| 206 | |||||||||||||
| 207 | Index des gestarteten Timers. | ||||||||||||
| 208 | |||||||||||||
| 209 | =back | ||||||||||||
| 210 | |||||||||||||
| 211 | =head4 Returns | ||||||||||||
| 212 | |||||||||||||
| 213 | Sekunden (Float) | ||||||||||||
| 214 | |||||||||||||
| 215 | =head4 Description | ||||||||||||
| 216 | |||||||||||||
| 217 | Starte Timer $i (setze ihn auf den aktuellen Zeitpunkt) und liefere | ||||||||||||
| 218 | die seit dem letzten Start vergangene Zeit zurück. | ||||||||||||
| 219 | |||||||||||||
| 220 | =cut | ||||||||||||
| 221 | |||||||||||||
| 222 | # ----------------------------------------------------------------------------- | ||||||||||||
| 223 | |||||||||||||
| 224 | 0 | ▶ | sub restart { | ||||||||||
|
Subroutine
restart: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 225 | 0 | my $self = shift; | |||||||||||
| 226 | 0 | ▶ | my $i = shift // 0; | ||||||||||
|
Condition0%0/2
shift() // 0
MC/DC0%0/1
shift() // 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 227 | |||||||||||||
| 228 | 0 | my $now = Time::HiRes::gettimeofday; | |||||||||||
| 229 | 0 | my $start = $self->[$i]; | |||||||||||
| 230 | 0 | $self->[$i] = $now; | |||||||||||
| 231 | |||||||||||||
| 232 | 0 | return $now-$start; | |||||||||||
| 233 | } | ||||||||||||
| 234 | |||||||||||||
| 235 | # ----------------------------------------------------------------------------- | ||||||||||||
| 236 | |||||||||||||
| 237 | =head1 VERSION | ||||||||||||
| 238 | |||||||||||||
| 239 | 1.240 | ||||||||||||
| 240 | |||||||||||||
| 241 | =head1 AUTHOR | ||||||||||||
| 242 | |||||||||||||
| 243 | Frank Seitz, L<http://fseitz.de/> | ||||||||||||
| 244 | |||||||||||||
| 245 | =head1 COPYRIGHT | ||||||||||||
| 246 | |||||||||||||
| 247 | Copyright (C) 2026 Frank Seitz | ||||||||||||
| 248 | |||||||||||||
| 249 | =head1 LICENSE | ||||||||||||
| 250 | |||||||||||||
| 251 | This code is free software; you can redistribute it and/or modify | ||||||||||||
| 252 | it under the same terms as Perl itself. | ||||||||||||
| 253 | |||||||||||||
| 254 | =cut | ||||||||||||
| 255 | |||||||||||||
| 256 | # ----------------------------------------------------------------------------- | ||||||||||||
| 257 | |||||||||||||
| 258 | 3 | 1; | |||||||||||
| 259 | |||||||||||||
| 260 | # eof | ||||||||||||