Skip to content

Commit

Permalink
#469 Add synthetic and cprofile benchmark with main entry
Browse files Browse the repository at this point in the history
  • Loading branch information
N720720 committed Jun 4, 2024
1 parent 7e3142c commit fc5b679
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
45 changes: 45 additions & 0 deletions benchmarking/cprofile_setup.py
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)
36 changes: 36 additions & 0 deletions benchmarking/synthetic_benchmark.py
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()

0 comments on commit fc5b679

Please sign in to comment.