1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors classes.struct combinators.smart fry kernel
4 math math.functions math.order math.parser sequences
5 specialized-arrays io ;
6 IN: benchmark.struct-arrays
8 STRUCT: point { x float } { y float } { z float } ;
10 SPECIALIZED-ARRAY: point
12 : xyz ( point -- x y z )
13 [ x>> ] [ y>> ] [ z>> ] tri ; inline
15 : change-xyz ( point obj x: ( x obj -- x' ) y: ( y obj -- y' ) z: ( z obj -- z' ) -- point )
16 tri-curry [ change-x ] [ change-y ] [ change-z ] tri* ; inline
18 : init-point ( n point -- n )
20 [ sin >>x ] [ cos 3 * >>y ] [ sin sq 2 / >>z ] tri drop
23 : make-points ( len -- points )
24 <point-array> dup 0 [ init-point ] reduce drop ; inline
26 : point-norm ( point -- norm )
27 [ xyz [ absq ] tri@ ] sum-outputs sqrt ; inline
29 : normalize-point ( point -- )
30 dup point-norm [ / ] [ / ] [ / ] change-xyz drop ; inline
32 : normalize-points ( points -- )
33 [ normalize-point ] each ; inline
35 : max-point ( point1 point2 -- point1 )
36 [ x>> max ] [ y>> max ] [ z>> max ] change-xyz ; inline
38 : <zero-point> ( -- point )
39 0 0 0 point <struct-boa> ; inline
41 : max-points ( points -- point )
42 <zero-point> [ max-point ] reduce ; inline
44 : print-point ( point -- )
45 [ xyz [ number>string ] tri@ ] output>array ", " join print ; inline
47 : struct-array-benchmark ( len -- )
48 make-points [ normalize-points ] [ max-points ] bi print-point ;
50 : main ( -- ) 5000000 struct-array-benchmark ;