From 624f6365330eb154e119c8ab040fb417394dad4f Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 28 Aug 2009 05:21:54 -0500 Subject: [PATCH] benchmark.struct-arrays: new benchmark to measure performance of struct-arrays, struct classes, and floating point math --- .../struct-arrays/struct-arrays.factor | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 extra/benchmark/struct-arrays/struct-arrays.factor diff --git a/extra/benchmark/struct-arrays/struct-arrays.factor b/extra/benchmark/struct-arrays/struct-arrays.factor new file mode 100644 index 0000000000..827604a39e --- /dev/null +++ b/extra/benchmark/struct-arrays/struct-arrays.factor @@ -0,0 +1,52 @@ +! Copyright (C) 2009 Slava Pestov. +! See http://factorcode.org/license.txt for BSD license. +USING: accessors classes.struct combinators.smart fry kernel +math math.functions math.order math.parser sequences +struct-arrays hints io ; +IN: benchmark.struct-arrays + +STRUCT: point { x float } { y float } { z float } ; + +: xyz ( point -- x y z ) + [ x>> ] [ y>> ] [ z>> ] tri ; inline + +: change-xyz ( point obj x: ( x obj -- x' ) y: ( y obj -- y' ) z: ( z obj -- z' ) -- point ) + tri-curry [ change-x ] [ change-y ] [ change-z ] tri* ; inline + +: init-point ( n point -- n ) + over >fixnum >float + [ sin >>x ] [ cos 3 * >>y ] [ sin sq 2 / >>z ] tri drop + 1 + ; inline + +: make-points ( len -- points ) + point dup 0 [ init-point ] reduce drop ; inline + +: point-norm ( point -- norm ) + [ xyz [ absq ] tri@ ] sum-outputs sqrt ; inline + +: normalize-point ( point -- ) + dup point-norm [ / ] [ / ] [ / ] change-xyz drop ; inline + +: normalize-points ( points -- ) + [ normalize-point ] each ; inline + +: max-point ( point1 point2 -- point1 ) + [ x>> max ] [ y>> max ] [ z>> max ] change-xyz ; inline + +: ( -- point ) + 0 0 0 point ; inline + +: max-points ( points -- point ) + [ max-point ] reduce ; inline + +: print-point ( point -- ) + [ xyz [ number>string ] tri@ ] output>array ", " join print ; inline + +: struct-array-benchmark ( len -- ) + make-points [ normalize-points ] [ max-points ] bi print-point ; + +HINTS: struct-array-benchmark fixnum ; + +: main ( -- ) 5000000 struct-array-benchmark ; + +MAIN: main