Created
November 29, 2018 19:22
-
-
Save huntiep/57dcf6d04ba814952e4ab7b5d1b51455 to your computer and use it in GitHub Desktop.
Benchmark of AMD64 PUSH vs SUB+MOV
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 = "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 |
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
#[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