From: Simon Cruanes <simon.cruanes.2007@m4x.org>
To: Alexey Egorov <alex.only.d@gmail.com>
Cc: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Optimizing pure-functional streams
Date: Tue, 11 Jul 2017 14:54:38 +0200 [thread overview]
Message-ID: <20170711125438.GP21228@nunchakus.loria.fr> (raw)
In-Reply-To: <CAJannG4mcHu6rDsb+LBfydCmhhN1F+OW7pUWDyWTnkvRP6r46w@mail.gmail.com>
[-- Attachment #1.1: Type: text/plain, Size: 1026 bytes --]
Hello,
Iterators in OCaml have been the topic of many discussions. Another
option for fast iterators is https://github.com/c-cube/sequence ,
which (with flambda) should compile down to loops and tests on this kind
of benchmark. With the attached additional file on 4.04.0+flambda,
I obtain the following (where sequence is test-seq):
$ for i in test-* ; do echo $i ; time ./$i ; done
test-c_loop
5000000100000000
./$i 0.08s user 0.00s system 97% cpu 0.085 total
test-f_loop
5000000100000000
./$i 0.10s user 0.00s system 96% cpu 0.100 total
test-loop
5000000100000000
./$i 0.18s user 0.00s system 97% cpu 0.184 total
test-seq
5000000100000000
./$i 0.11s user 0.00s system 97% cpu 0.113 total
test-stream
5000000100000000
./$i 0.44s user 0.00s system 98% cpu 0.449 total
Note that sequence is imperative underneath, but can be safely used as a
functional structure.
--
Simon Cruanes
http://weusepgp.info/
key 49AA62B6, fingerprint 949F EB87 8F06 59C6 D7D3 7D8D 4AC0 1D08 49AA 62B6
[-- Attachment #1.2: seq.ml --]
[-- Type: text/plain, Size: 474 bytes --]
type 'a seq = ('a -> unit) -> unit
let enum high k =
for i = 1 to high do k i done
[@@inline always]
let sum s =
let n = ref 0 in
s (fun x -> n := !n + x);
!n
[@@inline always]
let filter pred seq k =
seq (fun x -> if pred x then k x)
[@@inline always]
let map f seq k =
seq (fun x -> k (f x))
[@@inline always]
let high = 1000 * 1000 * 100
let res = enum high
|> filter (fun x -> x mod 2 = 0)
|> map (( * ) 2)
|> sum
let _ = Printf.printf "%d\n" res
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2017-07-11 12:54 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-07-10 20:26 Alexey Egorov
2017-07-10 21:29 ` Ivan Gotovchits
2017-07-10 22:28 ` Alexey Egorov
2017-07-11 12:40 ` Ivan Gotovchits
2017-07-11 12:54 ` Simon Cruanes [this message]
2017-07-11 17:37 ` Ivan Gotovchits
2017-07-11 17:46 ` Yotam Barnoy
2017-07-11 18:04 ` Gabriel Scherer
2017-07-11 18:15 ` Yotam Barnoy
2017-07-11 18:55 ` Ivan Gotovchits
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170711125438.GP21228@nunchakus.loria.fr \
--to=simon.cruanes.2007@m4x.org \
--cc=alex.only.d@gmail.com \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox