Repository navigation
Expand file tree
/
Copy pathbenchmark.py
More file actions
76 lines (58 loc) · 1.94 KB
/
Copy pathbenchmark.py
File metadata and controls
76 lines (58 loc) · 1.94 KB
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
import os
import time
from pathlib import Path
from dscan import scan_entries, scan
def bench_os_walk(root):
start = time.perf_counter()
count = 0
for root_dir, dirs, files in os.walk(root):
count += len(dirs) + len(files)
end = time.perf_counter()
return count, end - start
def bench_os_walk_stat(root):
start = time.perf_counter()
count = 0
for root_dir, dirs, files in os.walk(root):
for name in dirs + files:
try:
os.stat(os.path.join(root_dir, name))
count += 1
except OSError:
continue
end = time.perf_counter()
return count, end - start
def bench_pathlib_rglob(root):
start = time.perf_counter()
count = 0
for _ in Path(root).rglob("*"):
count += 1
end = time.perf_counter()
return count, end - start
def bench_dscan_entries(root):
start = time.perf_counter()
count = 0
for _ in scan_entries(root, ignore_dirs=[]):
count += 1
end = time.perf_counter()
return count, end - start
def bench_dscan_models(root):
start = time.perf_counter()
count = 0
for _ in scan(root, ignore_dirs=[]):
count += 1
end = time.perf_counter()
return count, end - start
if __name__ == "__main__":
import sys
path = sys.argv[1] if len(sys.argv) > 1 else "."
print(f"Benchmarking on {path}...")
count, duration = bench_os_walk(path)
print(f"os.walk (no stat): {count} entries, {duration:.4f}s")
count, duration = bench_os_walk_stat(path)
print(f"os.walk (+ stat): {count} entries, {duration:.4f}s")
count, duration = bench_pathlib_rglob(path)
print(f"pathlib.rglob: {count} entries, {duration:.4f}s")
count, duration = bench_dscan_entries(path)
print(f"dscan.scan_entries: {count} entries, {duration:.4f}s")
count, duration = bench_dscan_models(path)
print(f"dscan.scan (models/stat): {count} entries, {duration:.4f}s")