Skip to content

Commit

Permalink
chore: Benchmark utilizing tsc
Browse files Browse the repository at this point in the history
  • Loading branch information
nakedible authored Nov 8, 2023
1 parent 8d3c917 commit 84d5b2e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
27 changes: 25 additions & 2 deletions benches/basic.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::time::{Duration, Instant, SystemTime};

#[cfg(target_arch = "x86")]
use std::arch::x86;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64 as x86;

#[inline]
fn start_tsc() -> u64 {
unsafe { x86::_mm_lfence() };
let tsc = unsafe { x86::_rdtsc() };
unsafe { x86::_mm_lfence() };
tsc
}

fn stop_tsc() -> u64 {
let tsc = unsafe { x86::__rdtscp(&mut 0) };
unsafe { x86::_mm_lfence() };
tsc
}

fn bencher<I: Copy, O>(s: impl Fn() -> I, f: impl Fn(I) -> O) -> impl Fn(u64) -> Duration {
move |n| {
let v = s();
let now = Instant::now();
//let now = Instant::now();
let start = start_tsc();
for _ in 0..n {
let _ = black_box(f(v));
}
now.elapsed()
let end = stop_tsc();
let diff = end.saturating_sub(start);
Duration::from_nanos(diff)
//now.elapsed()
}
}

Expand Down
27 changes: 25 additions & 2 deletions benches/compare.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use std::time::{Duration, Instant, SystemTime};

#[cfg(target_arch = "x86")]
use std::arch::x86;
#[cfg(target_arch = "x86_64")]
use std::arch::x86_64 as x86;

#[inline]
fn start_tsc() -> u64 {
unsafe { x86::_mm_lfence() };
let tsc = unsafe { x86::_rdtsc() };
unsafe { x86::_mm_lfence() };
tsc
}

fn stop_tsc() -> u64 {
let tsc = unsafe { x86::__rdtscp(&mut 0) };
unsafe { x86::_mm_lfence() };
tsc
}

fn bencher<I: Copy, O>(s: impl Fn() -> I, f: impl Fn(I) -> O) -> impl Fn(u64) -> Duration {
move |n| {
let v = s();
let now = Instant::now();
//let now = Instant::now();
let start = start_tsc();
for _ in 0..n {
let _ = black_box(f(v));
}
now.elapsed()
let end = stop_tsc();
let diff = end.saturating_sub(start);
Duration::from_nanos(diff)
//now.elapsed()
}
}

Expand Down

0 comments on commit 84d5b2e

Please sign in to comment.