1 ! Copyright (C) 2009 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.data classes.struct combinators.smart
4 kernel math math.functions math.order math.parser sequences
5 specialized-arrays io ;
6 FROM: alien.c-types => float ;
7 IN: benchmark.struct-arrays
9 STRUCT: point { x float } { y float } { z float } ;
11 SPECIALIZED-ARRAY: point
13 : xyz ( point -- x y z )
14 [ x>> ] [ y>> ] [ z>> ] tri ; inline
16 : change-xyz ( point obj x: ( x obj -- x' ) y: ( y obj -- y' ) z: ( z obj -- z' ) -- point )
17 tri-curry [ change-x ] [ change-y ] [ change-z ] tri* ; inline
19 : init-point ( n point -- n )
21 [ sin >>x ] [ cos 3 * >>y ] [ sin sq 2 / >>z ] tri drop
24 : make-points ( len -- points )
25 point <c-array> dup 0 [ init-point ] reduce drop ; inline
27 : point-norm ( point -- norm )
28 [ xyz [ absq ] tri@ ] sum-outputs sqrt ; inline
30 : normalize-point ( point -- )
31 dup point-norm [ / ] [ / ] [ / ] change-xyz drop ; inline
33 : normalize-points ( points -- )
34 [ normalize-point ] each ; inline
36 : max-point ( point1 point2 -- point1 )
37 [ x>> max ] [ y>> max ] [ z>> max ] change-xyz ; inline
39 : <zero-point> ( -- point )
40 0 0 0 point boa ; inline
42 : max-points ( points -- point )
43 <zero-point> [ max-point ] reduce ; inline
45 : print-point ( point -- )
46 [ xyz [ number>string ] tri@ ] output>array ", " join print ; inline
48 : struct-arrays-bench ( len -- )
49 make-points [ normalize-points ] [ max-points ] bi print-point ;
51 : struct-arrays-benchmark ( -- )
52 10 [ 500000 struct-arrays-bench ] times ;
54 MAIN: struct-arrays-benchmark