Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
adding advent 2011 day 7
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #! http://perl6advent.wordpress.com/2011/12/07/grammarprofiler/ | ||
| use v6; | ||
| use Test; | ||
| use lib 't/spec/packages'; | ||
| use Advent::GrammarProfiler; | ||
| plan 3; | ||
|
|
||
| grammar MyGrammar { | ||
| rule TOP { <num> +% <op> } | ||
| token num { <[ 0..9 \. ]>+ } | ||
| token op { < + - * / = > } | ||
| } | ||
| ok MyGrammar.parse("37 + 10 - 5 = 42"), "parsed"; | ||
|
|
||
| my %timing = get-timing(); | ||
|
|
||
| ok %timing<MyGrammar><num><calls>, "num calls recorded"; | ||
| ok %timing<MyGrammar><num><time>, "time recorded"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| use v6; | ||
|
|
||
| our %timing; | ||
|
|
||
| my class ProfiledGrammarHOW is Metamodel::GrammarHOW is Mu { | ||
|
|
||
| method find_method($obj, $name) { | ||
| my $meth := callsame; | ||
| substr($name, 0, 1) eq '!' || $name eq any(<parse CREATE Bool defined MATCH perl>) ?? | ||
| $meth !! | ||
| -> $c, |args { | ||
| my $grammar = $obj.WHAT.perl; | ||
| %timing{$grammar} //= {}; # Vivify grammar hash | ||
| %timing{$grammar}{$meth.name} //= {}; # Vivify method hash | ||
| my %t := %timing{$grammar}{$meth.name}; | ||
| my $start = now; # get start time | ||
| my $result := $meth($obj, |args); # Call original method | ||
| %t<time> += now - $start; # accumulate execution time | ||
| %t<calls>++; | ||
| $result | ||
| } | ||
| } | ||
|
|
||
| method publish_method_cache($obj) { | ||
| # no caching, so we always hit find_method | ||
| } | ||
|
|
||
| } | ||
|
|
||
| sub get-timing is export { %timing } | ||
| sub reset-timing is export { %timing = {} } | ||
|
|
||
| my module EXPORTHOW { } | ||
| EXPORTHOW.WHO.<grammar> = ProfiledGrammarHOW; |