-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#469 Add synthetic and cprofile benchmark with main entry
- Loading branch information
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import cProfile | ||
import io | ||
import pstats | ||
from pathlib import Path | ||
|
||
import numpy as np | ||
|
||
from lindemann.main import main | ||
|
||
|
||
def profile_lindemann(args, number_of_runs=5): | ||
|
||
main(args) # Warmup | ||
all_times = [] | ||
for run_number in range(number_of_runs): | ||
|
||
profiler = cProfile.Profile() | ||
|
||
profiler.enable() | ||
main(args) | ||
profiler.disable() | ||
|
||
stats_stream = io.StringIO() | ||
stats = pstats.Stats(profiler, stream=stats_stream).sort_stats("tottime") | ||
stats.print_stats() | ||
|
||
stats_stream.seek(0) | ||
|
||
for line in stats_stream: | ||
if "tottime" in line: | ||
line = next(stats_stream) | ||
time = float(line.split()[1]) | ||
all_times.append(time) | ||
print(f"Time for run {run_number + 1}: {time} seconds") | ||
break | ||
|
||
all_times = np.array(all_times) | ||
mean_time = np.mean(all_times) | ||
std_time = np.std(all_times) | ||
print(f"Mean time of {number_of_runs} runs: {mean_time:.6f} s ± {std_time:.6f} s") | ||
|
||
|
||
if __name__ == "__main__": | ||
args = [Path("tests/test_example/459_01.lammpstrj")] | ||
profile_lindemann(args, number_of_runs=5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import time | ||
|
||
import numpy as np | ||
|
||
from lindemann.index import per_atoms, per_frames, per_trj | ||
|
||
|
||
def generate_test_data(num_frames, num_atoms): | ||
rng = np.random.default_rng(seed=42) | ||
return rng.random((num_frames, num_atoms, 3)).astype(np.float32) | ||
|
||
|
||
def benchmark(function, positions, iterations=3): | ||
index = function(positions) # Warm-up | ||
times = [] | ||
for _ in range(iterations): | ||
start_time = time.time() | ||
function(positions) | ||
end_time = time.time() | ||
times.append(end_time - start_time) | ||
return np.mean(times), np.std(times), index | ||
|
||
|
||
def main(): | ||
num_frames = 5000 | ||
num_atoms = 1103 | ||
positions = generate_test_data(num_frames, num_atoms) | ||
iterations = 1 | ||
|
||
mean_time, std_time, index = benchmark(per_trj.calculate, positions, iterations=iterations) | ||
print(f"Mean time of {iterations} runs: {mean_time:.6f} s ± {std_time:.6f} s") | ||
print(f"Index: {index}") | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |