import numpy as np import matplotlib.pyplot as plt class MemoryManager: instance = None stats = {} @staticmethod def get_instance(): if MemoryManager.instance is None: MemoryManager.instance = MemoryManager() return MemoryManager.instance @staticmethod def count_cells(): return len(MemoryManager.get_instance().cells) @staticmethod def count_reads(): return sum([cell.read_count for cell in MemoryManager.get_instance().cells]) @staticmethod def count_writes(): return sum([cell.write_count for cell in MemoryManager.get_instance().cells]) @staticmethod def count_compares(): return sum([cell.compare_count for cell in MemoryManager.get_instance().cells]) @staticmethod def count_adds(): return sum([cell.add_count for cell in MemoryManager.get_instance().cells]) @staticmethod def count_subs(): return sum([cell.sub_count for cell in MemoryManager.get_instance().cells]) @staticmethod def count_muls(): return sum([cell.mul_count for cell in MemoryManager.get_instance().cells]) @staticmethod def count_divs(): return sum([cell.div_count for cell in MemoryManager.get_instance().cells]) @staticmethod def count_bitops(): return sum([cell.bitop_count for cell in MemoryManager.get_instance().cells]) @staticmethod def reset(): manager = MemoryManager.get_instance() for cell in manager.cells: cell.reset_counters @staticmethod def purge(): MemoryManager.instance = None @staticmethod def save_stats(count): data = { "cells": MemoryManager.count_cells(), "reads": MemoryManager.count_reads(), "writes": MemoryManager.count_writes(), "compares": MemoryManager.count_compares(), "adds": MemoryManager.count_adds(), "subs": MemoryManager.count_subs(), "muls": MemoryManager.count_muls(), "divs": MemoryManager.count_divs(), "bitops": MemoryManager.count_bitops() } MemoryManager.stats[count] = data @staticmethod def plot_stats(labels): data = MemoryManager.stats x = list(data.keys()) fig, axes = plt.subplots(len(labels), 1, figsize=(8, 4 * len(labels)), sharex=True) if len(labels) == 1: axes = [axes] # Falls nur ein Plot vorhanden ist, in eine Liste umwandeln for ax, l in zip(axes, labels): y = [data[k][l] for k in x] ax.plot(x, y, label=l) ax.set_ylabel(l) ax.legend() plt.xlabel("n") plt.show() def __init__(self): self.cells = [] def add_cell(self, cell): self.cells.append(cell)