1
1
mirror of https://github.com/tweag/nickel.git synced 2024-10-04 15:17:34 +03:00

Merge pull request #765 from tweag/bench/lazy-array-contracts

Benchmark array contracts
This commit is contained in:
Yann Hamdaoui 2022-07-28 17:22:10 +02:00 committed by GitHub
commit 33f2007ec1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 0 deletions

View File

@ -109,6 +109,15 @@ config = Criterion::default().with_profiler(PProfProfiler::new(100, Output::Flam
name = "sort normal",
path = "arrays/sort",
args = (20),
}, {
name = "sum normal 50",
path = "arrays/sum",
args = (50),
}, {
name = "sum deepseq 50",
path = "arrays/sum",
args = (50),
eval_mode = EvalMode::DeepSeq,
}
}
criterion_main!(benches);

37
benches/arrays/primes.ncl Normal file
View File

@ -0,0 +1,37 @@
let range
| doc "Generate an array of integers in the range [`start`, `end`)."
| Num -> Num -> Array Num
= fun start end =>
if end <= start then
[]
else
array.generate (fun x => x + start) (end - start)
in
let is_prime
| doc "Returns true if the argument is a prime number."
= fun x => x > 1 && array.all (fun d => x % d != 0) (range 2 (x - 1))
in
let Prime = contract.from_predicate is_prime in
let primes
| doc "Generate `max` primes using Sieve of Eratosthenes."
| Num -> Array Prime
= fun max =>
let limit = num.pow max (1 / 2) in # sqrt(max)
let drop_multiples = fun x xs =>
let to_drop = max
|> array.generate (fun y => (y + 2) * x)
|> array.filter (fun y => y <= max) in
array.filter (fun y => array.all ((!=) y) to_drop) xs in
let rec loop = fun x xs =>
if x > limit then
xs
else
loop (x + 1) (drop_multiples x xs) in
loop 2 (range 2 max) in
{
run = primes
}

8
benches/arrays/sum.ncl Normal file
View File

@ -0,0 +1,8 @@
let sum
| Array Num -> Num
= fun xs =>
if array.length xs == 0 then 0
else array.head xs + sum (array.tail xs)
in {
run = fun n => array.generate (fun x => x + 1) n |> sum
}