]> gitweb.factorcode.org Git - factor.git/blob - extra/benchmark/struct-arrays/struct-arrays.factor
Specialized array overhaul
[factor.git] / extra / benchmark / struct-arrays / struct-arrays.factor
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
7
8 STRUCT: point { x float } { y float } { z float } ;
9
10 SPECIALIZED-ARRAY: point
11
12 : xyz ( point -- x y z )
13     [ x>> ] [ y>> ] [ z>> ] tri ; inline
14
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
17
18 : init-point ( n point -- n )
19     over >fixnum >float
20     [ sin >>x ] [ cos 3 * >>y ] [ sin sq 2 / >>z ] tri drop
21     1 + ; inline
22
23 : make-points ( len -- points )
24     <point-array> dup 0 [ init-point ] reduce drop ; inline
25
26 : point-norm ( point -- norm )
27     [ xyz [ absq ] tri@ ] sum-outputs sqrt ; inline
28
29 : normalize-point ( point -- )
30     dup point-norm [ / ] [ / ] [ / ] change-xyz drop ; inline
31
32 : normalize-points ( points -- )
33     [ normalize-point ] each ; inline
34
35 : max-point ( point1 point2 -- point1 )
36     [ x>> max ] [ y>> max ] [ z>> max ] change-xyz ; inline
37
38 : <zero-point> ( -- point )
39     0 0 0 point <struct-boa> ; inline
40
41 : max-points ( points -- point )
42     <zero-point> [ max-point ] reduce ; inline
43
44 : print-point ( point -- )
45     [ xyz [ number>string ] tri@ ] output>array ", " join print ; inline
46
47 : struct-array-benchmark ( len -- )
48     make-points [ normalize-points ] [ max-points ] bi print-point ;
49
50 : main ( -- ) 5000000 struct-array-benchmark ;
51
52 MAIN: main