Skip to content

Instantly share code, notes, and snippets.

@bluss
Created February 29, 2016 23:02
Show Gist options
  • Save bluss/ce593a7a75bd55896948 to your computer and use it in GitHub Desktop.
Save bluss/ce593a7a75bd55896948 to your computer and use it in GitHub Desktop.
rustc -C opt-level=3 --test deriveenum.rs && ./deriveenum --bench
#![allow(dead_code)]
#![feature(test)]
extern crate test;
use test::Bencher;
use std::mem;
#[derive(Copy, Clone, PartialEq)]
enum EnumX { A = 0, B, C, D, E, F, G, H, I, J }
fn eq_manual(x: &EnumX, y: &EnumX) -> bool {
*x as u8 == *y as u8
}
#[bench]
fn find_eq(b: &mut Bencher) {
let mut v = Vec::new();
for i in 0..255 {
unsafe {
let evalue: EnumX = mem::transmute(i % (EnumX::J as u8));
v.push(evalue);
}
}
b.iter(||{
// search for J using ==
v.iter().position(|elt| *elt == EnumX::J)
})
}
#[bench]
fn find_ne(b: &mut Bencher) {
let mut v = Vec::new();
for i in 0..255 {
unsafe {
let evalue: EnumX = mem::transmute(i % (EnumX::J as u8));
v.push(evalue);
}
}
b.iter(||{
// search for J using !=
v.iter().position(|elt| !(*elt != EnumX::J))
})
}
#[bench]
fn find_manual(b: &mut Bencher) {
let mut v = Vec::new();
for i in 0..255 {
unsafe {
let evalue: EnumX = mem::transmute(i % (EnumX::J as u8));
v.push(evalue);
}
}
b.iter(||{
// search for J using manual
v.iter().position(|elt| eq_manual(elt, &EnumX::J))
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment