-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
41 lines (33 loc) · 1.28 KB
/
Copy pathbenchmark.py
File metadata and controls
41 lines (33 loc) · 1.28 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
import os
import time
import concurrent.futures
import distributed_runtime
DATA_DIR = "dataset_mock"
def read_file_python(filepath):
with open(filepath, "rb") as f:
return len(f.read())
def benchmark_python(files):
start = time.time()
# Read files pure Python with thread pool
results = []
with concurrent.futures.ThreadPoolExecutor(max_workers=32) as executor:
results = list(executor.map(read_file_python, files))
end = time.time()
return end - start, len(results)
def benchmark_rust(files):
start = time.time()
# Pass off to Rust!
results = distributed_runtime.load_dataset_benchmark(files)
end = time.time()
return end - start, len(results)
if __name__ == "__main__":
files = [os.path.join(DATA_DIR, f) for f in os.listdir(DATA_DIR) if f.endswith(".txt")]
print(f"Found {len(files)} files. Starting Benchmark...\n")
# Run Pure Python
py_time, py_count = benchmark_python(files)
print(f"Pure Python (ThreadPool): loaded {py_count} files in {py_time:.3f} seconds")
# Run Rust Tokio
rs_time, rs_count = benchmark_rust(files)
print(f"Rust (Tokio Async): loaded {rs_count} files in {rs_time:.3f} seconds")
speedup = py_time / rs_time
print(f"\nRust was {speedup:.2f}x faster off the GIL!")