Skip to content

Instantly share code, notes, and snippets.

@quadrupleslap
Last active April 7, 2018 12:58
Show Gist options
  • Save quadrupleslap/06bf56be7aa68e94981bfc2d16883513 to your computer and use it in GitHub Desktop.
Save quadrupleslap/06bf56be7aa68e94981bfc2d16883513 to your computer and use it in GitHub Desktop.
A naive screen capture benchmark.
// cargo-deps: scrap, x11cap
// Use cargo-script to run this benchmark!
extern crate scrap;
extern crate x11cap;
use std::time::{Duration, Instant};
fn x11cap() {
use x11cap::{Capturer, CaptureSource};
println!("# x11cap");
let mut cap = Capturer::new(CaptureSource::Monitor(0)).unwrap();
let samples = 16;
let mut sum = Duration::new(0, 0);
for _ in 0..samples {
let start = Instant::now();
for _ in 0..3600 {
let frame = cap.capture_frame().unwrap();
let _ = frame.as_slice().len();
}
let elapsed = start.elapsed();
sum += elapsed;
println!("{} ns", ns(elapsed));
}
let time = ns(sum / samples);
println!("Average: {} ns", time);
println!("Framerate: {} fps", 3600 * 1_000_000_000 / time);
}
fn scrap() {
use scrap::{Display, Capturer};
println!("# scrap");
let mut scrap = Capturer::new(Display::primary().unwrap()).unwrap();
let samples = 16;
let mut sum = Duration::new(0, 0);
for _ in 0..samples {
let start = Instant::now();
for _ in 0..3600 {
let frame = scrap.frame().unwrap();
let _ = frame.len();
}
let elapsed = start.elapsed();
sum += elapsed;
println!("{} ns", ns(elapsed));
}
let time = ns(sum / samples);
println!("Average: {} ns", time);
println!("Framerate: {} fps", 3600 * 1_000_000_000 / time);
}
fn ns(t: Duration) -> u64 { t.as_secs() * 1_000_000_000 + t.subsec_nanos() as u64 }
fn main() {
x11cap();
scrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment