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 struct-arrays hints io ;
6 IN: benchmark.struct-arrays
8 STRUCT: point { x float } { y float } { z float } ;
10 : xyz ( point -- x y z )
11 [ x>> ] [ y>> ] [ z>> ] tri ; inline
13 : change-xyz ( point obj x: ( x obj -- x' ) y: ( y obj -- y' ) z: ( z obj -- z' ) -- point )
14 tri-curry [ change-x ] [ change-y ] [ change-z ] tri* ; inline
16 : init-point ( n point -- n )
18 [ sin >>x ] [ cos 3 * >>y ] [ sin sq 2 / >>z ] tri drop
21 : make-points ( len -- points )
22 point <struct-array> dup 0 [ init-point ] reduce drop ; inline
24 : point-norm ( point -- norm )
25 [ xyz [ absq ] tri@ ] sum-outputs sqrt ; inline
27 : normalize-point ( point -- )
28 dup point-norm [ / ] [ / ] [ / ] change-xyz drop ; inline
30 : normalize-points ( points -- )
31 [ normalize-point ] each ; inline
33 : max-point ( point1 point2 -- point1 )
34 [ x>> max ] [ y>> max ] [ z>> max ] change-xyz ; inline
36 : <zero-point> ( -- point )
37 0 0 0 point <struct-boa> ; inline
39 : max-points ( points -- point )
40 <zero-point> [ max-point ] reduce ; inline
42 : print-point ( point -- )
43 [ xyz [ number>string ] tri@ ] output>array ", " join print ; inline
45 : struct-array-benchmark ( len -- )
46 make-points [ normalize-points ] [ max-points ] bi print-point ;
48 HINTS: struct-array-benchmark fixnum ;
50 : main ( -- ) 5000000 struct-array-benchmark ;