| 1 | # | ||||||||||||||||||
| 2 | # Copyright (c) 2013-2022 Christian Jaeger, copying@christianjaeger.ch | ||||||||||||||||||
| 3 | # | ||||||||||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||||||||||
| 7 | # bundled with this file. | ||||||||||||||||||
| 8 | # | ||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | # Depends: () | ||||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||
| 13 | |||||||||||||||||||
| 14 | Chj::time_this - benchmarking function that also returns the result(s) | ||||||||||||||||||
| 15 | |||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||
| 17 | |||||||||||||||||||
| 18 | use Chj::tim; | ||||||||||||||||||
| 19 | my $res = time_this { somefunc(66) }; # prints timing to stderr | ||||||||||||||||||
| 20 | # or | ||||||||||||||||||
| 21 | my $res = time_this { somefunc(66) } "somefunc"; # included in message | ||||||||||||||||||
| 22 | # or | ||||||||||||||||||
| 23 | my $res = time_this { somefunc(66) } | ||||||||||||||||||
| 24 | msg => "somefunc", n => 10; # run thunk 10 times | ||||||||||||||||||
| 25 | # or | ||||||||||||||||||
| 26 | my $res = time_this { somefunc(66) } out => \@t; # push to @t instead of stderr | ||||||||||||||||||
| 27 | |||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||
| 29 | |||||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | Currently does not divide the timings by the number of iterations. | ||||||||||||||||||
| 32 | |||||||||||||||||||
| 33 | Currently does not subtract the overhead of calling the thunk (as | ||||||||||||||||||
| 34 | Benchmark.pm does, but can't use it since it doesn't return values; | ||||||||||||||||||
| 35 | should we wrap and use assignment instead? But then timings are off | ||||||||||||||||||
| 36 | again.) | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | Also should probably follow the output format of Benchmark.pm | ||||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | =head1 SEE ALSO | ||||||||||||||||||
| 41 | |||||||||||||||||||
| 42 | L<Benchmark> | ||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | =head1 NOTE | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | This is alpha software! Read the status section in the package README | ||||||||||||||||||
| 47 | or on the L<website|http://functional-perl.org/>. | ||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | =cut | ||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | package Chj::time_this; | ||||||||||||||||||
| 52 | 4 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 53 | 4 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 54 | 4 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 55 | 4 | ▶ | use Exporter "import"; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | 4 | our @EXPORT = qw(time_this); | |||||||||||||||||
| 58 | 4 | our @EXPORT_OK = qw(); | |||||||||||||||||
| 59 | 4 | our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]); | |||||||||||||||||
| 60 | |||||||||||||||||||
| 61 | 4 | my $fields = [qw(user system cuser csystem)]; | |||||||||||||||||
| 62 | |||||||||||||||||||
| 63 | 7 | ▶ | sub time_this (&;@) { | ||||||||||||||||
|
Subroutine
time_this: called CC=11 SCAR=26.8
Pod
undocumented
| |||||||||||||||||||
| 64 | 7 | my ($thunk, @args) = @_; | |||||||||||||||||
| 65 | 7 | my $wantarray = wantarray; ## no critic | |||||||||||||||||
| 66 | 7 | my $args = {}; | |||||||||||||||||
| 67 | my $maybe_msg | ||||||||||||||||||
| 68 | 7 | ▶ | = @args == 1 ? $args[0] : do { $args = +{@args}; $$args{msg} }; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 69 | 7 | ▶ | my $n = $$args{n} // 1; | ||||||||||||||||
|
Condition50%1/2
$args->{"n"} // 1
MC/DC0%0/1
$args->{"n"} // 1
$args->{"n"}
Truth table50%1/2
A
A= $args->{"n"}
| |||||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | 7 | my $a = [times]; | |||||||||||||||||
| 72 | 7 | my @res; | |||||||||||||||||
| 73 | 7 | for (1 .. $n) { | |||||||||||||||||
| 74 | 7 | ▶ | @res = $wantarray ? &$thunk() : scalar &$thunk(); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 75 | } | ||||||||||||||||||
| 76 | 7 | my $b = [times]; | |||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 7 | my $d = [map { $$fields[$_] . " = " . ($$b[$_] - $$a[$_]) } 0 .. $#$a]; | |||||||||||||||||
| 79 | 7 | ▶ | my $forstr = defined($maybe_msg) ? " for $maybe_msg" : ""; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 80 | 7 | my $msgstr = "times$forstr: " . join(", ", @$d) . "\n"; | |||||||||||||||||
| 81 | 7 | ▶ | if (my $out = $$args{out}) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 82 | 0 | ▶ | if (ref($out) eq "ARRAY") { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 83 | 0 | push @$out, $msgstr | |||||||||||||||||
| 84 | } elsif (ref($out) eq "SCALAR") { | ||||||||||||||||||
| 85 | 0 | $$out = $msgstr | |||||||||||||||||
| 86 | } elsif (is_filehandle $out) { | ||||||||||||||||||
| 87 | 0 | print $out $msgstr | |||||||||||||||||
| 88 | } else { | ||||||||||||||||||
| 89 | 0 | warn "don't know how to output to '$out'"; | |||||||||||||||||
| 90 | } | ||||||||||||||||||
| 91 | } else { | ||||||||||||||||||
| 92 | 7 | warn $msgstr; | |||||||||||||||||
| 93 | } | ||||||||||||||||||
| 94 | 7 | ▶ | $wantarray ? @res : $res[0] | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 95 | } | ||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | 4 | 1 | |||||||||||||||||