Skip to content

Commit

Permalink
return value/use extra::test::black_box in benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
lpy authored and alexcrichton committed Feb 14, 2014
1 parent 18477ac commit 665555d
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 22 deletions.
8 changes: 8 additions & 0 deletions src/libcollections/bitv.rs
Expand Up @@ -1561,6 +1561,7 @@ mod tests {
let mut bitv = 0 as uint;
b.iter(|| {
bitv |= (1 << ((r.next_u32() as uint) % uint::BITS));
&bitv
})
}

Expand All @@ -1570,6 +1571,7 @@ mod tests {
let mut bitv = SmallBitv::new(uint::BITS);
b.iter(|| {
bitv.set((r.next_u32() as uint) % uint::BITS, true);
&bitv
})
}

Expand All @@ -1579,6 +1581,7 @@ mod tests {
let mut bitv = BigBitv::new(~[0]);
b.iter(|| {
bitv.set((r.next_u32() as uint) % uint::BITS, true);
&bitv
})
}

Expand All @@ -1590,6 +1593,7 @@ mod tests {
let mut bitv = BigBitv::new(storage);
b.iter(|| {
bitv.set((r.next_u32() as uint) % BENCH_BITS, true);
&bitv
})
}

Expand All @@ -1599,6 +1603,7 @@ mod tests {
let mut bitv = Bitv::new(BENCH_BITS, false);
b.iter(|| {
bitv.set((r.next_u32() as uint) % BENCH_BITS, true);
&bitv
})
}

Expand All @@ -1608,6 +1613,7 @@ mod tests {
let mut bitv = Bitv::new(uint::BITS, false);
b.iter(|| {
bitv.set((r.next_u32() as uint) % uint::BITS, true);
&bitv
})
}

Expand All @@ -1617,6 +1623,7 @@ mod tests {
let mut bitv = BitvSet::new();
b.iter(|| {
bitv.insert((r.next_u32() as uint) % uint::BITS);
&bitv
})
}

Expand All @@ -1626,6 +1633,7 @@ mod tests {
let mut bitv = BitvSet::new();
b.iter(|| {
bitv.insert((r.next_u32() as uint) % BENCH_BITS);
&bitv
})
}

Expand Down
3 changes: 2 additions & 1 deletion src/libcollections/deque.rs
Expand Up @@ -115,8 +115,9 @@ pub mod bench {
// measure
let mut i = 0;
bh.iter(|| {
map.find(&i);
let x = map.find(&i);
i = (i + 1) % n;
x
})
}
}
4 changes: 2 additions & 2 deletions src/libstd/io/buffered.rs
Expand Up @@ -565,14 +565,14 @@ mod test {
#[bench]
fn bench_buffered_reader(bh: &mut Harness) {
bh.iter(|| {
BufferedReader::new(NullStream);
BufferedReader::new(NullStream)
});
}

#[bench]
fn bench_buffered_writer(bh: &mut Harness) {
bh.iter(|| {
BufferedWriter::new(NullStream);
BufferedWriter::new(NullStream)
});
}

Expand Down
12 changes: 6 additions & 6 deletions src/libstd/mem.rs
Expand Up @@ -292,15 +292,15 @@ mod bench {
let s = Struct { field: 10 };
let t = &s as &Trait;
bh.iter(|| {
t.method();
t.method()
});
}

#[bench]
fn trait_static_method_call(bh: &mut BenchHarness) {
let s = Struct { field: 10 };
bh.iter(|| {
s.method();
s.method()
});
}

Expand All @@ -310,21 +310,21 @@ mod bench {
fn match_option_some(bh: &mut BenchHarness) {
let x = Some(10);
bh.iter(|| {
let _q = match x {
match x {
Some(y) => y,
None => 11
};
}
});
}

#[bench]
fn match_vec_pattern(bh: &mut BenchHarness) {
let x = [1,2,3,4,5,6];
bh.iter(|| {
let _q = match x {
match x {
[1,2,3,..] => 10,
_ => 11
};
}
});
}
}
4 changes: 2 additions & 2 deletions src/libstd/rt/global_heap.rs
Expand Up @@ -112,14 +112,14 @@ mod bench {
#[bench]
fn alloc_owned_small(bh: &mut BenchHarness) {
bh.iter(|| {
~10;
~10
})
}

#[bench]
fn alloc_owned_big(bh: &mut BenchHarness) {
bh.iter(|| {
~[10, ..1000];
~[10, ..1000]
})
}
}
6 changes: 3 additions & 3 deletions src/libstd/str.rs
Expand Up @@ -4357,7 +4357,7 @@ mod bench {

assert_eq!(100, s.len());
bh.iter(|| {
let _ = is_utf8(s);
is_utf8(s)
});
}

Expand All @@ -4366,7 +4366,7 @@ mod bench {
let s = bytes!("𐌀𐌖𐌋𐌄𐌑𐌉ปรدولة الكويتทศไทย中华𐍅𐌿𐌻𐍆𐌹𐌻𐌰");
assert_eq!(100, s.len());
bh.iter(|| {
let _ = is_utf8(s);
is_utf8(s)
});
}

Expand Down Expand Up @@ -4409,7 +4409,7 @@ mod bench {
#[bench]
fn bench_with_capacity(bh: &mut BenchHarness) {
bh.iter(|| {
let _ = with_capacity(100);
with_capacity(100)
});
}

Expand Down
19 changes: 11 additions & 8 deletions src/libstd/vec.rs
Expand Up @@ -4428,22 +4428,23 @@ mod bench {
let mut vec: ~[uint] = ~[0u];
bh.iter(|| {
vec.push(0);
&vec
})
}

#[bench]
fn starts_with_same_vector(bh: &mut BenchHarness) {
let vec: ~[uint] = vec::from_fn(100, |i| i);
bh.iter(|| {
vec.starts_with(vec);
vec.starts_with(vec)
})
}

#[bench]
fn starts_with_single_element(bh: &mut BenchHarness) {
let vec: ~[uint] = ~[0u];
bh.iter(|| {
vec.starts_with(vec);
vec.starts_with(vec)
})
}

Expand All @@ -4453,23 +4454,23 @@ mod bench {
let mut match_vec: ~[uint] = vec::from_fn(99, |i| i);
match_vec.push(0);
bh.iter(|| {
vec.starts_with(match_vec);
vec.starts_with(match_vec)
})
}

#[bench]
fn ends_with_same_vector(bh: &mut BenchHarness) {
let vec: ~[uint] = vec::from_fn(100, |i| i);
bh.iter(|| {
vec.ends_with(vec);
vec.ends_with(vec)
})
}

#[bench]
fn ends_with_single_element(bh: &mut BenchHarness) {
let vec: ~[uint] = ~[0u];
bh.iter(|| {
vec.ends_with(vec);
vec.ends_with(vec)
})
}

Expand All @@ -4479,15 +4480,15 @@ mod bench {
let mut match_vec: ~[uint] = vec::from_fn(100, |i| i);
match_vec[0] = 200;
bh.iter(|| {
vec.starts_with(match_vec);
vec.starts_with(match_vec)
})
}

#[bench]
fn contains_last_element(bh: &mut BenchHarness) {
let vec: ~[uint] = vec::from_fn(100, |i| i);
bh.iter(|| {
vec.contains(&99u);
vec.contains(&99u)
})
}

Expand All @@ -4507,13 +4508,14 @@ mod bench {
ptr::set_memory(vp, 0, 1024);
v.set_len(1024);
}
v
});
}

#[bench]
fn zero_1kb_fixed_repeat(bh: &mut BenchHarness) {
bh.iter(|| {
let _v: ~[u8] = ~[0u8, ..1024];
~[0u8, ..1024]
});
}

Expand Down Expand Up @@ -4542,6 +4544,7 @@ mod bench {
for x in v.mut_iter() {
*x = 0;
}
v
});
}

Expand Down

0 comments on commit 665555d

Please sign in to comment.