summaryrefslogtreecommitdiffstats
path: root/openat/benches/count_processes.rs
blob: aa9f8568b5866758d07af162c2a3532e6583f0fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#![feature(test)]

extern crate openat;
extern crate test;


use std::fs::read_dir;
use std::str::from_utf8;
use std::os::unix::ffi::OsStrExt;
use test::Bencher;

use openat::Dir;


#[bench]
fn procs_stdlib(b: &mut Bencher) {
    b.iter(|| {
        read_dir("/proc").unwrap().filter(|r| {
            r.as_ref().ok()
            .and_then(|e| from_utf8(e.file_name().as_bytes()).ok()
            // pid is everything that can be parsed as a number
                .and_then(|s| s.parse::<u32>().ok()))
            .is_some()
        }).count()
    });
}

#[bench]
fn procs_openat(b: &mut Bencher) {
    b.iter(|| {
        Dir::open("/proc").unwrap().list_dir(".").unwrap().filter(|r| {
            r.as_ref().ok()
            .and_then(|e| from_utf8(e.file_name().as_bytes()).ok()
            // pid is everything that can be parsed as a number
                .and_then(|s| s.parse::<u32>().ok()))
            .is_some()
        }).count()
    });
}