-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbenchmark_performance.py
More file actions
133 lines (99 loc) · 3.81 KB
/
benchmark_performance.py
File metadata and controls
133 lines (99 loc) · 3.81 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
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
#!/usr/bin/env python3
"""Performance benchmarking script for METAINFORMANT improvements.
This script demonstrates the performance improvements from caching,
vectorization, and optimization enhancements.
"""
from __future__ import annotations
import time
from pathlib import Path
import numpy as np
from metainformant.information.syntactic import shannon_entropy
from metainformant.math import coalescent
from metainformant.math.price import covariance, variance
def benchmark_tajima_constants():
"""Benchmark Tajima constants calculation with caching."""
print("=== Tajima Constants Performance Benchmark ===")
sample_sizes = [10, 50, 100, 10, 50, 100] # Repeated calculations to test caching
start_time = time.time()
results = []
for n in sample_sizes:
constants = coalescent.tajima_constants(n)
results.append(constants["a1"])
total_time = time.time() - start_time
print(".4f")
print(".4f")
print("Note: Second set of calculations should be faster due to caching")
return total_time
def benchmark_vectorization():
"""Benchmark NumPy vectorization improvements."""
print("\n=== Vectorization Performance Benchmark ===")
# Create large test datasets
sizes = [1000, 10000, 100000]
results = {}
for size in sizes:
data = np.random.randn(size)
# Benchmark variance
start = time.time()
var_result = variance(data)
var_time = time.time() - start
# Benchmark covariance
data2 = np.random.randn(size)
start = time.time()
cov_result = covariance(data, data2)
cov_time = time.time() - start
results[size] = {
"variance_time": var_time,
"covariance_time": cov_time,
"variance_result": var_result,
"covariance_result": cov_result,
}
print("2d")
return results
def benchmark_entropy_caching():
"""Benchmark entropy calculation with caching."""
print("\n=== Entropy Caching Performance Benchmark ===")
# Test with different probability distributions
distributions = [
[0.5, 0.5], # Binary
[0.25, 0.25, 0.25, 0.25], # Uniform 4
[0.1, 0.2, 0.3, 0.4], # Non-uniform
[0.5, 0.5], # Repeat to test caching
[0.25, 0.25, 0.25, 0.25], # Repeat to test caching
]
start_time = time.time()
results = []
for i, probs in enumerate(distributions):
entropy = shannon_entropy(probs)
results.append(entropy)
print(".4f")
total_time = time.time() - start_time
print(".4f")
print("Note: Repeated distributions should be faster due to caching")
return total_time, results
def main():
"""Run all performance benchmarks."""
print("METAINFORMANT Performance Benchmark Suite")
print("=" * 50)
try:
# Benchmark Tajima constants
tajima_time = benchmark_tajima_constants()
# Benchmark vectorization
vectorization_results = benchmark_vectorization()
# Benchmark entropy caching
entropy_time, entropy_results = benchmark_entropy_caching()
# Summary
print("\n=== Performance Summary ===")
print(f"Tajima constants total time: {tajima_time:.4f}s")
print(f"Vectorization tested on datasets up to {max(vectorization_results.keys())} samples")
print(f"Entropy calculations total time: {entropy_time:.4f}s")
print("\nAll benchmarks completed successfully!")
print("\nKey improvements:")
print("- Caching reduces repeated computation time")
print("- NumPy vectorization enables large dataset processing")
print("- Progress tracking provides user feedback for long operations")
except Exception as e:
print(f"Benchmark failed: {e}")
return 1
return 0
if __name__ == "__main__":
exit(main())