Skip to content
Snippets Groups Projects
Unverified Commit 84f6c843 authored by Timothy's avatar Timothy
Browse files

Add benchmark

parent 4d5163e0
No related branches found
No related tags found
No related merge requests found
Benchmarks against a straight loop
----------------------------------
$ lscpu | grep "Model name"
Model name: Intel(R) Core(TM) i5-5200U CPU @ 2.20GHz
$ cargo bench
test bench_oddness ... bench: 398,832 ns/iter (+/- 261,575)
test bench_oddness_loop ... bench: 2 ns/iter (+/- 0)
#![feature(test)]
extern crate tramp;
extern crate test;
use tramp::oddness as oddness;
use test::Bencher;
#[bench]
fn bench_oddness(b: &mut Bencher) {
let mut val : u128 = 1000;
b.iter(|| {oddness::is_even(val); val += 1;});
}
#[bench]
fn bench_oddness_loop(b: &mut Bencher) {
let mut val : u128 = 1000;
b.iter(|| {oddness::is_even_loop(val); val += 1;});
}
......@@ -180,3 +180,5 @@ macro_rules! rec_ret {
return $crate::BorrowRec::Ret($val);
};
}
pub mod oddness;
use super::{Rec, tramp};
fn is_even_rec(x: u128) -> Rec<bool> {
if x > 0 {
rec_call!(is_odd_rec(x - 1))
} else {
rec_ret!(true)
}
}
fn is_odd_rec(x: u128) -> Rec<bool> {
if x > 0 {
rec_call!(is_even_rec(x - 1))
} else {
rec_ret!(false)
}
}
pub fn is_even(x: u128) -> bool {
tramp(is_even_rec(x))
}
pub fn is_odd(x: u128) -> bool {
tramp(is_odd_rec(x))
}
pub fn is_even_loop(x: u128) -> bool {
let mut i: u128 = x;
let mut even = true;
while i > 0 {
i = i - 1;
even = !even;
}
even
}
pub fn is_odd_loop(x: u128) -> bool {
!is_even_loop(x)
}
#[macro_use]
extern crate tramp;
// Not the greatest way of computing "is even" or "is odd".
mod oddness {
use tramp::{tramp, Rec};
fn is_even_rec(x: u128) -> Rec<bool> {
if x > 0 {
rec_call!(is_odd_rec(x - 1))
} else {
rec_ret!(true)
}
}
fn is_odd_rec(x: u128) -> Rec<bool> {
if x > 0 {
rec_call!(is_even_rec(x - 1))
} else {
rec_ret!(false)
}
}
pub fn is_even(x: u128) -> bool {
tramp(is_even_rec(x))
}
pub fn is_odd(x: u128) -> bool {
tramp(is_odd_rec(x))
}
}
use tramp::oddness;
#[test]
fn test_oddness() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment