-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from FL03/v0.1.19
V0.1.19
- Loading branch information
Showing
30 changed files
with
430 additions
and
269 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// bench.rs | ||
#![feature(test)] | ||
|
||
extern crate test; | ||
|
||
use std::mem::replace; | ||
use test::Bencher; | ||
|
||
// bench: find the `BENCH_SIZE` first terms of the fibonacci sequence | ||
static BENCH_SIZE: usize = 20; | ||
|
||
// recursive fibonacci | ||
fn fibonacci(n: usize) -> u32 { | ||
if n < 2 { | ||
1 | ||
} else { | ||
fibonacci(n - 1) + fibonacci(n - 2) | ||
} | ||
} | ||
|
||
// iterative fibonacci | ||
struct Fibonacci { | ||
curr: u32, | ||
next: u32, | ||
} | ||
|
||
impl Iterator for Fibonacci { | ||
type Item = u32; | ||
fn next(&mut self) -> Option<u32> { | ||
let new_next = self.curr + self.next; | ||
let new_curr = replace(&mut self.next, new_next); | ||
|
||
Some(replace(&mut self.curr, new_curr)) | ||
} | ||
} | ||
|
||
fn fibonacci_sequence() -> Fibonacci { | ||
Fibonacci { curr: 1, next: 1 } | ||
} | ||
|
||
// function to benchmark must be annotated with `#[bench]` | ||
#[bench] | ||
fn recursive_fibonacci(b: &mut Bencher) { | ||
// exact code to benchmark must be passed as a closure to the iter | ||
// method of Bencher | ||
b.iter(|| (0..BENCH_SIZE).map(fibonacci).collect::<Vec<u32>>()) | ||
} | ||
|
||
#[bench] | ||
fn iterative_fibonacci(b: &mut Bencher) { | ||
b.iter(|| fibonacci_sequence().take(BENCH_SIZE).collect::<Vec<u32>>()) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// bench.rs | ||
#![feature(test)] | ||
|
||
extern crate test; | ||
use algae::graph::{DirectedGraph, Edge, Graph,}; | ||
use test::Bencher; | ||
|
||
const TEST_EDGES: [(&str, &str, usize); 5] = [ | ||
("a", "b", 5), | ||
("c", "a", 7), | ||
("b", "c", 10), | ||
("d", "c", 10), | ||
("e", "f", 10), | ||
]; | ||
|
||
#[bench] | ||
fn bench_directed(b: &mut Bencher) { | ||
let mut graph = DirectedGraph::<&str, usize>::new(); | ||
b.iter(|| { | ||
TEST_EDGES | ||
.into_iter() | ||
.map(|i| Edge::from(i)) | ||
.for_each(|i| graph.add_edge(i)); | ||
}); | ||
} |
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
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
Oops, something went wrong.