]> gitweb.factorcode.org Git - factor.git/commitdiff
Add new words to tools.annotations to annotate words with timing code
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sat, 6 Dec 2008 06:20:49 +0000 (00:20 -0600)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sat, 6 Dec 2008 06:20:49 +0000 (00:20 -0600)
basis/tools/annotations/annotations-docs.factor
basis/tools/annotations/annotations-tests.factor
basis/tools/annotations/annotations.factor

index c61b4547a94a0226e6184d10080a08ffdb872433..acb6d6dd2ae7a022c8ceab097fa2fa55dc812cb9 100644 (file)
@@ -4,9 +4,17 @@ IN: tools.annotations
 
 ARTICLE: "tools.annotations" "Word annotations"
 "The word annotation feature modifies word definitions to add debugging code. You can restore the old definition by calling " { $link reset } " on the word in question."
+$nl
+"Printing messages when a word is called or returns:"
 { $subsection watch }
+{ $subsection watch-vars }
+"Starting the walker when a word is called:"
 { $subsection breakpoint }
 { $subsection breakpoint-if }
+"Timing words:"
+{ $subsection reset-word-timing }
+{ $subsection add-timing }
+{ $subsection word-timing. }
 "All of the above words are implemented using a single combinator which applies a quotation to a word definition to yield a new definition:"
 { $subsection annotate } ;
 
@@ -63,3 +71,13 @@ HELP: word-inputs
      { "seq" sequence } }
 { $description "Makes a sequence of the inputs to a word by counting the number of inputs in the stack effect and saving that many items from the datastack." } ;
 
+HELP: add-timing
+{ $values { "word" word } }
+{ $description "Adds timing code to a word, which records its total running time, including that of words it calls, on every invocation." }
+{ $see-also "tools.time" } ;
+
+HELP: reset-word-timing
+{ $description "Resets the word timing table." } ;
+
+HELP: word-timing.
+{ $description "Prints the word timing table." } ;
index 1e1eccb8b592dbe1d24c74361b394ca67225c86b..1e766e3dec2e279b1a5596308529d260264dd343 100644 (file)
@@ -1,4 +1,4 @@
-USING: tools.test tools.annotations math parser eval
+USING: tools.test tools.annotations tools.time math parser eval
 io.streams.string kernel ;
 IN: tools.annotations.tests
 
index 9847b16bc2f3066a29453323fa9e5c43a9adb54c..e5f6af22677f8d6ebc8c3a4e120228d8d2c029b2 100644 (file)
@@ -1,9 +1,9 @@
 ! Copyright (C) 2005, 2008 Slava Pestov.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: accessors kernel words parser io summary quotations
-sequences prettyprint continuations effects definitions
-compiler.units namespaces assocs tools.walker generic
-inspector fry ;
+USING: accessors kernel math sorting words parser io summary
+quotations sequences prettyprint continuations effects
+definitions compiler.units namespaces assocs tools.walker
+tools.time generic inspector fry ;
 IN: tools.annotations
 
 GENERIC: reset ( word -- )
@@ -20,9 +20,11 @@ M: word reset
         f "unannotated-def" set-word-prop
     ] [ drop ] if ;
 
+ERROR: cannot-annotate-twice word ;
+
 : annotate ( word quot -- )
     over "unannotated-def" word-prop [
-        "Cannot annotate a word twice" throw
+        over cannot-annotate-twice
     ] when
     [
         over dup def>> "unannotated-def" set-word-prop
@@ -82,3 +84,21 @@ M: word annotate-methods
 
 : breakpoint-if ( word quot -- )
     '[ [ _ [ [ break ] when ] ] dip 3append ] annotate-methods ;
+
+SYMBOL: word-timing
+
+H{ } clone word-timing set-global
+
+: reset-word-timing ( -- )
+    word-timing get clear-assoc ;
+
+: (add-timing) ( def word -- def' )
+    '[ _ benchmark _ word-timing get at+ ] ;
+
+: add-timing ( word -- )
+    dup '[ _ (add-timing) ] annotate ;
+
+: word-timing. ( -- )
+    word-timing get
+    >alist [ 1000000 /f ] assoc-map sort-values
+    simple-table. ;