Paste: http://www.wagerlabs.com/blog/2008/01/introducing-fac.html

Author: cadar777
Mode: factor
Date: Sun, 9 Nov 2008 15:08:02
Plain Text |
USING: kernel io.encodings.binary io.files io.binary math math.order grouping sequences io.mmap accessors alien.c-types io tools.time  ; 

IN: my

: from-source ( -- stream ) "/dev/random" binary <file-reader> ;
: to-target ( -- stream ) "/tmp/floats.bin" binary <file-writer> ;

: make-float ( n -- f ) le> bits>float ;
: clip ( n -- n ) 1000.0 min -1000.0 max ;

: sum-floats ( -- o )
    "/tmp/floats.bin" 4000000 [
        4 <sliced-groups> 0 [ make-float clip + 2 / ] reduce
    ] with-mapped-file ;

: sum-floats1 ( -- o )
     "/tmp/floats.bin" 4000000 [ 
         0 swap dup length 4 / swap address>> 
         [ float-nth clip + 2 / ] curry each
     ] with-mapped-file ;


: r ( -- stream ) from-source [ 4000000 read ] with-input-stream ;
: w ( stream -- ) to-target  [ write ] with-output-stream ;

: init ( -- ) r w ;
: test1 ( -- )  [ sum-floats ] time ;
: test2 ( -- )  [ sum-floats1 ] time ;

MAIN: init

New Annotation

Summary:
Author:
Mode:
Body: