Skip to content

Commit

Permalink
add new 'count' example to count as fast as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jan 7, 2024
1 parent 1a0d6bf commit f2b33bd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
35 changes: 35 additions & 0 deletions examples/dc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//! Collect the amount of directories and files as fast as possible.
mod shared;

use clap::Parser;
use jwalk::WalkDirGeneric;

fn main() {
let args = shared::Args::parse();

let parallelism = args.parallelism();
let threads = args.threads();
let path = args.root.unwrap_or_else(|| ".".into());
let (mut dirs, mut files, mut symlinks) = (0, 0, 0);
for dir_entry_result in WalkDirGeneric::<((), Option<u64>)>::new(&path)
.skip_hidden(false)
.parallelism(parallelism)
{
match dir_entry_result {
Ok(dir_entry) => {
if dir_entry.file_type.is_dir() {
dirs += 1;
} else if dir_entry.file_type.is_file() {
files += 1;
} else if dir_entry.file_type.is_symlink() {
symlinks += 1
}
}
Err(error) => {
println!("Read dir_entry error: {}", error);
}
}
}

println!("dirs: {dirs}, files: {files}, symlinks: {symlinks} (threads: {threads})");
}
14 changes: 8 additions & 6 deletions examples/shared/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ pub struct Args {
}

impl Args {
pub fn parallelism(&self) -> Parallelism {
let threads = self
.threads
pub fn threads(&self) -> usize {
self.threads
.unwrap_or_else(|| {
if cfg!(darwin) {
if cfg!(target_vendor = "apple") {
NonZeroUsize::new(4).unwrap()
} else {
std::thread::available_parallelism().unwrap_or(NonZeroUsize::new(1).unwrap())
}
})
.get();
match threads {
.get()
}

pub fn parallelism(&self) -> Parallelism {
match self.threads() {
1 => Parallelism::Serial,
n => Parallelism::RayonNewPool(n),
}
Expand Down

0 comments on commit f2b33bd

Please sign in to comment.