Created
July 10, 2017 20:09
-
-
Save anonymous/127e9116b362d561c5dfa9cce6b453f3 to your computer and use it in GitHub Desktop.
OCaml stream benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#define HIGH (1000 * 1000 * 100) | |
long long c_loop(int high) { | |
long long total = 0; | |
int i; | |
for(i = 1; i <= high; ++i) | |
if(i % 2 == 0) | |
total += i * 2; | |
return total; | |
} | |
int main(void){ | |
printf("%lld\n", c_loop(HIGH)); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let loop high = | |
let res = ref 0 in | |
for i = 1 to high do | |
if i mod 2 = 0 then res := !res + i * 2 | |
done; | |
!res | |
let high = 1000 * 1000 * 100 | |
let _ = Printf.printf "%d\n" (loop high) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let loop high = | |
let rec loop = function | |
| i, t when i > high -> t | |
| i, t when i mod 2 = 0 -> loop (i + 1, t + i * 2) | |
| i, t -> loop (i + 1, t) in | |
loop (1, 0) | |
let high = 1000 * 1000 * 100 | |
let _ = Printf.printf "%d\n" (loop high) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OCAMLOPT = ocamlopt -principal -O3 | |
CC = gcc -O3 -Wall | |
all: | |
$(OCAMLOPT) loop.ml -o test-loop | |
$(OCAMLOPT) f_loop.ml -o test-f_loop | |
$(OCAMLOPT) stream.ml -o test-stream | |
$(CC) c_loop.c -o test-c_loop | |
clean: | |
rm -f test-* *.o *.cmx *.cmi | |
.PHONY: all clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ('s, 'a) s = | |
| Done | |
| Skip of 's | |
| Yield of 's * 'a | |
type _ t = Iterator : 's * ('s -> ('s, 'a) s) -> 'a t | |
let enum high = | |
let f = function | |
| n when n > high -> Done | |
| n -> Yield (n + 1, n) [@@inline always] in | |
Iterator (1, f) | |
[@@inline always] | |
let sum (Iterator (s, next)) = | |
let rec loop t s = | |
match next s with | |
| Done -> t | |
| Skip s -> loop t s | |
| Yield (s, x) -> loop (t + x) s [@@inline always] in | |
loop 0 s | |
[@@inline always] | |
let filter pred (Iterator (s, next)) = | |
let f s = | |
match next s with | |
| Done | Skip _ as x -> x | |
| Yield (_, x) as y when pred x -> y | |
| Yield (s, _) -> Skip s [@@inline always] in | |
Iterator (s, f) | |
[@@inline always] | |
let map f (Iterator (s, next)) = | |
let g s = | |
match next s with | |
| Done | Skip _ as x -> x | |
| Yield (s, x) -> Yield (s, f x) [@@inline always] in | |
Iterator (s, g) | |
[@@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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment