]> gitweb.factorcode.org Git - factor.git/commitdiff
project-euler.ave-time: fix ave-time combinator to infer with new, stricter stack...
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Mon, 9 Nov 2009 06:54:59 +0000 (00:54 -0600)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Mon, 9 Nov 2009 06:54:59 +0000 (00:54 -0600)
extra/project-euler/ave-time/ave-time-docs.factor
extra/project-euler/ave-time/ave-time-tests.factor [new file with mode: 0644]
extra/project-euler/ave-time/ave-time.factor

index f2d6b89afcb1c215768cc7bdf3ca7e35aa4d3df0..1fb41b61c0d799f135b1d522fc7c2fb65908bbe3 100644 (file)
@@ -9,14 +9,6 @@ HELP: collect-benchmarks
     $nl
     "A nicer word for interactive use is " { $link ave-time } "." } ;
 
-HELP: nth-place
-{ $values { "x" float } { "n" integer } { "y" float } }
-{ $description "Rounds a floating point number to " { $snippet "n" } " decimal places." }
-{ $examples
-    "This word is useful for display purposes when showing 15 decimal places is not desired:"
-    { $unchecked-example "3.141592653589793 3 nth-place number>string" "\"3.142\"" }
-} ;
-
 HELP: ave-time
 { $values { "quot" quotation } { "n" integer } }
 { $description "Runs a quotation " { $snippet "n" } " times, then prints the average run time and standard deviation." }
diff --git a/extra/project-euler/ave-time/ave-time-tests.factor b/extra/project-euler/ave-time/ave-time-tests.factor
new file mode 100644 (file)
index 0000000..86b0048
--- /dev/null
@@ -0,0 +1,5 @@
+IN: project-euler.ave-time.tests
+USING: tools.test math arrays project-euler.ave-time ;
+
+{ 0 3 } [ 1 2 [ + ] 10 collect-benchmarks ] must-infer-as
+[ 1 2 t ] [ 1 2 [ + ] 10 collect-benchmarks array? ] unit-test
index cc326c1afe9bd40c14e5c4e914d07cdd81116e0a..ec190fed187da15b7c51c00c970a1b1cc855f3c2 100644 (file)
@@ -1,24 +1,16 @@
 ! Copyright (c) 2007, 2008 Aaron Schaefer.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: continuations fry io kernel make math math.functions
-math.parser math.statistics memory tools.time ;
+USING: combinators.smart formatting fry io kernel macros math
+math.functions math.statistics memory sequences tools.time ;
 IN: project-euler.ave-time
 
-: nth-place ( x n -- y )
-    10^ [ * round >integer ] keep /f ;
-
-: collect-benchmarks ( quot n -- seq )
-    [
-        [ datastack ]
-        [
-            '[ _ gc benchmark 1000 / , ]
-            [ '[ _ _ with-datastack drop ] ] keep swap
-        ]
-        [ 1 - ] tri* swap times call
-    ] { } make ; inline
+MACRO: collect-benchmarks ( quot n -- seq )
+    swap '[ _ [ [ [ _ nullary ] preserving ] gc benchmark 1000 / ] replicate ] ;
 
 : ave-time ( quot n -- )
-    [ collect-benchmarks ] keep swap
-    [ std 2 nth-place ] [ mean round >integer ] bi [
-        # " ms ave run time - " % # " SD (" % # " trials)" %
-    ] "" make print flush ; inline
+    [
+        collect-benchmarks
+        [ mean round >integer ]
+        [ std ] bi
+    ] keep
+    "%d ms ave run time - %.2f SD (%d trials)\n" printf flush ; inline