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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//! xlsx を読む速さを測る
//!
//! ファイルを 1 つ、またはディレクトリを渡す。ディレクトリなら、中の xlsx を全部読む。
//! どのファイルもシートと行を全部読み、かかった時間を出す。
//!
//! 2 つ目にスレッドの数を渡すと、ファイルをそのスレッドに分けて同時に読む。
//! 小さなファイルを数多く読むときは、これがいちばん効く。
//! メモリの最大量も見たいときは、macOS なら 3 行目のように実行する。
//!
//! cargo run --release --example scale -- sample/scale.xlsx
//! cargo run --release --example scale -- sample/small 8
//! /usr/bin/time -l ./target/release/examples/scale sample/small
use folio::Error;
use folio::book::Book;
use folio::cell::Value;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Instant;
/// 読んだものの数え
#[derive(Default)]
struct Count {
row: u64,
cell: u64,
/// 数の合計
total: f64,
/// 文字のバイト数
text: u64,
}
impl Count {
/// ファイルを 1 つ読んで数える
fn read(&mut self, path: &Path) -> Result<(), Error> {
let mut book = Book::open(path)?;
let name: Vec<String> = book.sheet().iter().map(|sheet| sheet.name.clone()).collect();
for want in &name {
let mut sheet = book.read(want)?;
while let Some(row) = sheet.read()? {
self.row += 1;
for cell in &row.cell {
self.cell += 1;
match &cell.value {
Value::Number(number) => self.total += number,
Value::Text(value) => self.text += value.len() as u64,
_ => {}
}
}
}
}
Ok(())
}
/// 別のスレッドで数えたものを足す
fn add(&mut self, other: Count) {
self.row += other.row;
self.cell += other.cell;
self.total += other.total;
self.text += other.text;
}
}
fn main() {
if let Err(error) = run() {
eprintln!("{error}");
std::process::exit(1);
}
}
fn run() -> Result<(), Error> {
let mut argument = std::env::args().skip(1);
let Some(path) = argument.next() else {
return Err(Error::new("読むファイルかディレクトリを渡してください"));
};
let thread: usize = argument.next().and_then(|text| text.parse().ok()).unwrap_or(1).max(1);
let path = Path::new(&path);
let mut file = Vec::new();
if path.is_dir() {
for entry in std::fs::read_dir(path)? {
let one = entry?.path();
if one.extension().is_some_and(|value| value == "xlsx") {
file.push(one);
}
}
file.sort();
} else {
file.push(path.to_path_buf());
}
let start = Instant::now();
let count = read_all(&file, thread)?;
let elapsed = start.elapsed().as_secs_f64();
let each = elapsed * 1000.0 / file.len().max(1) as f64;
println!(
"{} ファイル {} 行 {} セル {thread} スレッド {elapsed:.3} 秒 1 ファイルあたり {each:.3} ミリ秒",
file.len(),
count.row,
count.cell
);
println!("数の合計 {} 文字のバイト数 {}", count.total, count.text);
Ok(())
}
/// ファイルを thread 本のスレッドに分けて読む。どのスレッドも、次に読むファイルを順に取っていく
fn read_all(file: &[PathBuf], thread: usize) -> Result<Count, Error> {
let next = AtomicUsize::new(0);
let work = || -> Result<Count, Error> {
let mut count = Count::default();
loop {
let index = next.fetch_add(1, Ordering::Relaxed);
let Some(path) = file.get(index) else {
return Ok(count);
};
count.read(path)?;
}
};
if thread == 1 {
return work();
}
std::thread::scope(|scope| {
let handle: Vec<_> = (0..thread).map(|_| scope.spawn(work)).collect();
let mut total = Count::default();
for one in handle {
match one.join() {
Ok(result) => total.add(result?),
Err(_) => return Err(Error::new("読むスレッドが途中で止まりました")),
}
}
Ok(total)
})
}