# Paste: speed1

Author: iris1 factor 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 factor 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 factor 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 factor 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 factor 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: textPostgreSQLPowerCenter Parameter Fileactionscriptadaada95ans-forth94antantlrantlr4apacheconfapdlapplescriptaspaspect-jassembly-agcassembly-agsassembly-arm32assembly-m68kassembly-macro32assembly-mcs51assembly-parrotassembly-r2000assembly-x86avroawkbbatchbbjbcelbeanshellbibtexbinsource-agccc#c++cfscriptchillcilclipsclojurecmakecobolcoffeescriptcoldfusioncplex-lpcsscsvcvs-commitddartdjangodockerfiledotdoxygendrawj2ddsssleiffelembperlerlangfactorfhtmlforthfortranfortran90foxprofreemarkergcbasicgettextgherkingnuplotgogradlegroovygsphaskellhaxehexhl7v2hlslhtaccesshtmlhxmli4glicalendariconidlinforminiinno-setupinterlisiojamonjavajava module-infojavaccjavafxjavascriptjcljedit-actionsjflexjhtmljmkjsonjspkotlinlatexlexlilypondlispliterate-haskelllogslogtalklotosluam4macroschedulermailmakefilemaplemarkdownmavenmavscriptmetapostmlmodula3moinmqscmustache-templatemxmlmyghtymysqln3netrexxnqcnsis2objective-cobjectrexxoccamomnimarkopensipsoutlinepascalpatchperlphppikepl-sqlpl-sql9pl1plaintexplantumlpop11postscriptpovraypowerdynamopowershellprogressprologpropertiesprotobufpspptlpurepvwavepyrexpythonqdocrakefilercprdrebolredcoderelax-ng-compactrenderman-ribrestrfcrhtmlroffrpm-specrtfrubyrustrviews#s+sassbtscalaschemesdl/prsgmlshellscriptshtmlsipsippslateslaxsmalltalksmartysmi-mibsparqlsql-loadersqrsquidconfstatasvn-commitswiftswigtcltemplate-toolkittextexinfotldtransact-sqltsptwikityposcripturluscriptvalavbscriptvelocityverilogvhdlvisualbasicvrml2wellknowntextxmlxqxslyabyamlzpt