Paste: speed1

Author: iris1
Mode: factor
Date: Sat, 11 Sep 2010 13:50:47
Plain Text |
! Generate 1000 random vectors
! Compute pairwise distances
! Output the smallest one

USING: math math.combinatorics math.order math.vectors math.functions random sequences tools.time ; 

IN: speed1

CONSTANT: test-size 1000

: u01 ( -- u ) 0. 1. uniform-random-float ;

: rv ( -- v ) 3 [ u01 ] replicate ; 

: rvs ( n -- vv ) [ rv ] replicate ; 

: dist ( v w -- n ) v- norm-sq sqrt ; 

: main-test ( -- result seconds ) [ test-size rvs 2 1. [ first2 dist min ] reduce-combinations ] benchmark 1e9 / ;

MAIN: main-test

Annotation: TYPED: in action

Author: pruned
Mode: factor
Date: Sat, 11 Sep 2010 17:01:12
Plain Text |
QUALIFIED: alien.c-types 
STRUCT: float-3 { xyz alien.c-types:float[3] } ;
SPECIALIZED-ARRAY: float-3

TYPED:: min-distance ( points: float-3-array -- dist )
points length iota :> iter
1/0. :> mn!
iter [| i | iter [| j | i j = [ i points nth j points nth [ xyz>> ] bi@ distance mn min mn! ] unless ] each ] each
mn ;


( scratchpad ) 10000
[
  3 <mersenne-twister>
  [ [ 3 [ 0 1 uniform-random-float ] replicate >float-array float-3 <struct-boa> ] float-3-array{ } replicate-as ] with-random
  min-distance .
] time
0.001421769884698972
Running time: 4.681582435 seconds

Annotation: simd

Author: pruned
Mode: factor
Date: Sat, 11 Sep 2010 17:08:45
Plain Text |
! QUALIFIED: alien.c-types 
! STRUCT: float-3 { xyz alien.c-types:float[3] } ;
SPECIALIZED-ARRAY: float-4

TYPED:: min-distance ( points: float-4-array -- dist )
points length iota :> iter
1/0. :> mn!
iter [| i | iter [| j | i j = [ i points nth j points nth distance mn min mn! ] unless ] each ] each
mn ;

10000
[
  3 <mersenne-twister>
  [ [ 3 [ 0 1 uniform-random-float ] replicate 0 suffix >float-array >float-4 ] float-4-array{ } replicate-as ] with-random
  min-distance .
] time
0.001421769850965218
Running time: 2.801278933 seconds

Annotation: little tweak

Author: pruned
Mode: factor
Date: Sat, 11 Sep 2010 17:20:45
Plain Text |
SPECIALIZED-ARRAY: float-4

TYPED:: min-distance ( points: float-4-array -- dist )
points length iota :> iter
1/0. :> mn!
iter [| i | iter [| j | i j = [ i points nth j points nth distance mn { float } declare min mn! ] unless ] each ] each
mn ;

10000
[
  3 <mersenne-twister>
  [ [ 3 [ 0 1 uniform-random-float ] replicate 0 suffix >float-array >float-4 ] float-4-array{ } replicate-as ] with-random
  min-distance .
] time
0.001421769850965218
Running time: 1.645217479 seconds

Annotation: doh, symmetry

Author: pruned
Mode: factor
Date: Sat, 11 Sep 2010 17:38:28
Plain Text |
SPECIALIZED-ARRAY: float-4

TYPED:: min-distance ( points: float-4-array -- dist )
points length :> N
1/0. :> mn!
N iota [| i | i iota [| j | i points nth j points nth distance mn { float } declare min mn! ] each ] each
mn ;

10000
[
  3 <mersenne-twister>
  [ [ 3 [ 0 1 uniform-random-float ] replicate 0 suffix >float-array >float-4 ] float-4-array{ } replicate-as ] with-random
  min-distance .
] time
0.001421769850965218
Running time: 0.8185219070000001 seconds

New Annotation

Summary:
Author:
Mode:
Body: