-
-
Save DanielKeep/7a252390423a47582771 to your computer and use it in GitHub Desktop.
Struct copy benchmark
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
target | |
Cargo.lock |
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
#![feature(test)] | |
extern crate bench_yich; | |
extern crate test; | |
use bench_yich::*; | |
use test::Bencher; | |
const ARRAY_SIZE: usize = 2621; // ~ 1 MB | |
#[bench] | |
fn bench_one_rust(b: &mut Bencher) { | |
let src = Lump::default(); | |
let mut dst = Lump::default(); | |
b.iter(|| { | |
copy_struct_one_rust(&src, &mut dst) | |
}); | |
} | |
#[bench] | |
fn bench_one_c(b: &mut Bencher) { | |
let src = Lump::default(); | |
let mut dst = Lump::default(); | |
b.iter(|| unsafe { | |
copy_struct_one_c(&src, &mut dst) | |
}); | |
} | |
#[bench] | |
fn bench_many_rust(b: &mut Bencher) { | |
let src = vec![Lump::default(); ARRAY_SIZE]; | |
let mut dst = src.clone(); | |
b.iter(|| { | |
copy_struct_many_rust(&src, &mut dst) | |
}); | |
} | |
#[bench] | |
fn bench_many_c(b: &mut Bencher) { | |
let src = vec![Lump::default(); ARRAY_SIZE]; | |
let mut dst = src.clone(); | |
{ | |
let n = std::cmp::min(src.len(), dst.len()); | |
let src = src.as_ptr(); | |
let dst = dst.as_mut_ptr(); | |
b.iter(|| unsafe { | |
copy_struct_many_c(src, dst, n) | |
}); | |
} | |
} |
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
extern crate gcc; | |
fn main() { | |
gcc::compile_library("libyichc.a", &["lib.c"]); | |
} |
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
[package] | |
name = "bench-yich" | |
version = "0.1.0" | |
authors = ["Daniel Keep <daniel.keep@gmail.com>"] | |
build = "build.rs" | |
[build-dependencies] | |
gcc = "0.3.20" | |
[lib] | |
name = "bench_yich" | |
path = "lib.rs" | |
[[bench]] | |
name = "benches" | |
path = "benches.rs" |
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
#include <stdint.h> | |
typedef struct Lump { | |
uint8_t data[400]; | |
} Lump; | |
extern void copy_struct_one_c(const Lump *src, Lump *dst) { | |
*dst = *src; | |
} | |
extern void copy_struct_many_c(const Lump *src, Lump *dst, size_t n) { | |
size_t i; | |
for(i = 0; i < n; ++i) { | |
dst[i] = src[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
#[derive(Copy)] | |
#[repr(C)] | |
pub struct Lump(pub [u8; 400]); | |
impl Clone for Lump { | |
fn clone(&self) -> Self { | |
*self | |
} | |
} | |
impl Default for Lump { | |
fn default() -> Self { | |
Lump([0; 400]) | |
} | |
} | |
#[inline(never)] | |
pub fn copy_struct_one_rust(src: &Lump, dst: &mut Lump) { | |
*dst = *src; | |
} | |
#[inline(never)] | |
pub fn copy_struct_many_rust(src: &[Lump], dst: &mut [Lump]) { | |
let mut src = src.iter(); | |
let mut dst = dst.iter_mut(); | |
while let (Some(src), Some(dst)) = (src.next(), dst.next()) { | |
*dst = *src; | |
} | |
} | |
extern "C" { | |
pub fn copy_struct_one_c(src: *const Lump, dst: *mut Lump); | |
pub fn copy_struct_many_c(src: *const Lump, dst: *mut Lump, n: usize); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment