| 1 | ######################################################################## | ||
| 2 | # housekeeping | ||
| 3 | ######################################################################## | ||
| 4 | package LinkedList::Single::Example::Bench v2.0.0; | ||
| 5 | use v5.40; | ||
| 6 | |||
| 7 | use B::Deparse; | ||
| 8 | use Devel::Size qw( size ); | ||
| 9 | use Time::HiRes qw( time ); | ||
| 10 | |||
| 11 | use Exporter 'import'; | ||
| 12 | |||
| 13 | our @EXPORT_OK | ||
| 14 | = qw | ||
| 15 | ( | ||
| 16 | array_bytes | ||
| 17 | best_of | ||
| 18 | complexity | ||
| 19 | list_bytes | ||
| 20 | report | ||
| 21 | ); | ||
| 22 | |||
| 23 | ######################################################################## | ||
| 24 | # time | ||
| 25 | ######################################################################## | ||
| 26 | |||
| 27 | # smallest elapsed time of $reps runs: the least disturbed by | ||
| 28 | # whatever else the system happens to be doing. | ||
| 29 | |||
| 30 | sub best_of( $reps, $code ) | ||
| 31 | { | ||
| 32 | my $best; | ||
| 33 | |||
| 34 | for( 1 .. $reps ) | ||
| 35 | { | ||
| 36 | my $start = time; | ||
| 37 | |||
| 38 | $code->(); | ||
| 39 | |||
| 40 | my $took = time - $start; | ||
| 41 | |||
| 42 | if( ! defined $best || $took < $best ) | ||
| 43 | { | ||
| 44 | $best = $took; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | $best | ||
| 49 | } | ||
| 50 | |||
| 51 | ######################################################################## | ||
| 52 | # code complexity | ||
| 53 | # | ||
| 54 | # a heuristic, not a metric to be proud of: deparse the sub so that | ||
| 55 | # formatting does not matter, then count what makes code harder to | ||
| 56 | # follow: statements, branches, and the position arithmetic that | ||
| 57 | # arrays require and linked lists replace with a cursor. | ||
| 58 | ######################################################################## | ||
| 59 | |||
| 60 | sub complexity( $code ) | ||
| 61 | { | ||
| 62 | my $text | ||
| 63 | = B::Deparse->new->coderef2text( $code ); | ||
| 64 | |||
| 65 | my $statements | ||
| 66 | = () = $text =~ m{ ; }gx; | ||
| 67 | |||
| 68 | my $branches | ||
| 69 | = () = $text =~ m{ \b (?: if | elsif | unless | while | until | for | foreach | and | or ) \b | \? }gx; | ||
| 70 | |||
| 71 | # element access ($a[0], $$r[0], $r->[0]), $#array, and splice. | ||
| 72 | |||
| 73 | my $index_ops | ||
| 74 | = () = $text =~ m{ (?<= [\w\}\]] ) \[ | \$\# | \bsplice\b }gx; | ||
| 75 | |||
| 76 | return | ||
| 77 | { | ||
| 78 | statements => $statements | ||
| 79 | , branches => $branches | ||
| 80 | , index_ops => $index_ops | ||
| 81 | }; | ||
| 82 | } | ||
| 83 | |||
| 84 | ######################################################################## | ||
| 85 | # memory | ||
| 86 | # | ||
| 87 | # container overhead only: the bytes the structure holds in order to | ||
| 88 | # hold the payload, not the payload itself. a perl array keeps the | ||
| 89 | # capacity it grew to after shift/pop; a list holds only live nodes. | ||
| 90 | ######################################################################## | ||
| 91 | |||
| 92 | sub array_bytes( $aref ) | ||
| 93 | { | ||
| 94 | size $aref | ||
| 95 | } | ||
| 96 | |||
| 97 | # Devel::Size cannot see into the list object (it reports the | ||
| 98 | # few bytes of the handle), so walk the nodes and add them up. | ||
| 99 | |||
| 100 | sub list_bytes( $list ) | ||
| 101 | { | ||
| 102 | my $bytes = 0; | ||
| 103 | |||
| 104 | for | ||
| 105 | ( | ||
| 106 | my $node = $list->node | ||
| 107 | ; $node->@* | ||
| 108 | ; $node = $node->[0] | ||
| 109 | ) | ||
| 110 | { | ||
| 111 | $bytes += size $node; | ||
| 112 | } | ||
| 113 | |||
| 114 | $bytes | ||
| 115 | } | ||
| 116 | |||
| 117 | ######################################################################## | ||
| 118 | # report | ||
| 119 | ######################################################################## | ||
| 120 | |||
| 121 | sub report( $title, @row ) | ||
| 122 | { | ||
| 123 | say "\n$title"; | ||
| 124 | |||
| 125 | say sprintf '%-14s %9s %12s %6s %6s %6s' | ||
| 126 | => qw( case seconds bytes stmts branch index ); | ||
| 127 | |||
| 128 | for my $row ( @row ) | ||
| 129 | { | ||
| 130 | my $cx = $row->{ cx }; | ||
| 131 | |||
| 132 | say sprintf '%-14s %9.4f %12s %6d %6d %6d' | ||
| 133 | => $row->{ name } | ||
| 134 | , $row->{ secs } | ||
| 135 | , $row->{ bytes } // '-' | ||
| 136 | , $cx->{ statements } | ||
| 137 | , $cx->{ branches } | ||
| 138 | , $cx->{ index_ops }; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | 1 | ||
| 143 | __END__ |