Skip to content

Instantly share code, notes, and snippets.

@huntiep huntiep/Cargo.toml
Created Nov 29, 2018

Embed
What would you like to do?
Benchmark of AMD64 PUSH vs SUB+MOV
[package]
name = "push"
version = "0.1.0"
authors = ["A non-e-moose"]
[dependencies]
[dev-dependencies]
criterion = "0.2.5"
memmap = "0.7.0"
[[bench]]
name = "push"
harness = false
#[macro_use]
extern crate criterion;
extern crate memmap;
use criterion::Criterion;
use memmap::MmapMut;
fn push(c: &mut criterion::Criterion) {
let code = vec![0x50, // push %rax
0x58, // pop %rax
0xc3]; // ret
let mut m = MmapMut::map_anon(code.len()).unwrap();
for (i, c) in code.iter().enumerate() {
m[i] = *c;
}
let m = m.make_exec().unwrap();
let f = unsafe { std::mem::transmute::<*const u8, fn() -> ()>(m.as_ptr()) };
c.bench_function("push", move |b| b.iter(|| f()));
}
fn mov(c: &mut criterion::Criterion) {
let code = vec![0x48, 0x83, 0xec, 0x08, // sub $8, %rsp
0x48, 0x89, 0x04, 0x24, // mov %rax, (%rsp)
0x48, 0x8b, 0x04, 0x24, // mov (%rsp), %rax
0x48, 0x83, 0xc4, 0x08, // add $8, %rsp
0xc3]; // ret
let mut m = MmapMut::map_anon(code.len()).unwrap();
for (i, c) in code.iter().enumerate() {
m[i] = *c;
}
let m = m.make_exec().unwrap();
let f = unsafe { std::mem::transmute::<*const u8, fn() -> ()>(m.as_ptr()) };
c.bench_function("mov", move |b| b.iter(|| f()));
}
criterion_group!(benches, push, mov);
criterion_main!(benches);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.