-
Notifications
You must be signed in to change notification settings - Fork 520
/
main.rs
71 lines (58 loc) · 1.19 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#![feature(test)]
extern crate test;
use test::Bencher;
fn main() {
println!("Hello, world!");
}
pub fn square_bit(s: u32) -> u64 {
if (s < 1) | (s > 64) {
panic!("Square must be between 1 and 64");
}
1 << (s - 1)
}
pub fn total_bit() -> u64 {
((1_u128 << 64) - 1) as u64
}
pub fn square_pow(s: u32) -> u64 {
if (s < 1) | (s > 64) {
panic!("Square must be between 1 and 64");
}
2u64.pow(s - 1)
}
pub fn total_pow_u128() -> u64 {
(2_u128.pow(64) - 1) as u64
}
pub fn total_pow_fold() -> u64 {
(1_u32..=64_u32).fold(0u64, |total, num| total + square_pow(num))
}
pub fn total_pow_for() -> u64 {
let mut accum = 0;
for i in 1..=64 {
accum += square_pow(i)
}
accum
}
#[bench]
fn square_bit(b: &mut Bencher) {
b.iter(|| square_bit(64));
}
#[bench]
fn total_bit(b: &mut Bencher) {
b.iter(|| total_bit());
}
#[bench]
fn square_pow(b: &mut Bencher) {
b.iter(|| square_pow(64));
}
#[bench]
fn total_pow_u128(b: &mut Bencher) {
b.iter(|| total_pow_u128());
}
#[bench]
fn total_pow_fold(b: &mut Bencher) {
b.iter(|| total_pow_fold());
}
#[bench]
fn total_pow_for(b: &mut Bencher) {
b.iter(|| total_pow_for());
}