Projet_Boites/latex/annex-performance.tex
2023-06-04 08:43:26 +02:00

57 lines
1.5 KiB
TeX

For simplicity, we only include the script for the improved algorithm. For the
intuitive algorithm, simply replace the algorithm. The imports, timing and memory
usage tracking code are nearly identical.
\begin{lstlisting}[language=python]
#!/usr/bin/python3
import tracemalloc
from random import random
from math import floor, sqrt
#from statistics import mean, variance
from time import perf_counter
# starting the memory monitoring
tracemalloc.start()
start_time = perf_counter()
# store memory consumption before
current_before, peak_before = tracemalloc.get_traced_memory()
# algorithm (part to replace)
N = 10**6
Tot = 0
Tot2 = 0
for _ in range(N):
item = random()
Tot += item
Tot2 += item ** 2
mean = Tot / N
variance = Tot2 / (N-1) - mean**2
# store memory after
current_after, peak_after = tracemalloc.get_traced_memory()
end_time = perf_counter()
print("mean :", mean)
print("variance :", variance)
# displaying the memory usage
print("Used memory before : {} B (current), {} B (peak)".format(current_before,peak_before))
print("Used memory after : {} B (current), {} B (peak)".format(current_after,peak_after))
print("Used memory : {} B".format(peak_after - current_before))
print("Time : {} ms".format((end_time - start_time) * 1000))
tracemalloc.stop()
\end{lstlisting}
Example output:
\begin{lstlisting}[language=python]
mean : 0.5002592040785124
variance : 0.0833757719902084
Used memory before : 0 B (current), 0 B (peak)
Used memory after : 1308 B (current), 1336 B (peak)
Used memory : 1336 B
Time : 535.1873079998768 ms
\end{lstlisting}