from utils.memory_manager import MemoryManager from utils.memory_array import MemoryArray from utils.literal import Literal from b_tree import BTree from b_tree_node import BTreeNode class MemoryManagerBTree(MemoryManager): """ Diese Klasse erweitert den MemoryManager, um spezifische Statistiken für B-Bäume zu speichern. """ @staticmethod def count_loads(): return sum([cell.loaded_count for cell in MemoryManager().cells if isinstance(cell, BTreeNode)]) @staticmethod def count_saves(): return sum([cell.saved_count for cell in MemoryManager().cells if isinstance(cell, BTreeNode)]) @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(), "loads": MemoryManagerBTree.count_loads(), "saves": MemoryManagerBTree.count_saves() } MemoryManager.stats[count] = data def analyze_complexity(sizes): """ Analysiert die Komplexität :param sizes: Eine Liste von Eingabegrößen für die Analyse. """ for size in sizes: MemoryManager.purge() # Speicher zurücksetzen tree = BTree(5) random_array = MemoryArray.create_random_array(size, -100, 100) for i in range(size-1): tree.insert(int(random_array[Literal(i)])) MemoryManager.reset() tree.insert(int(random_array[Literal(size-1)])) MemoryManagerBTree.save_stats(size) MemoryManager.plot_stats(["cells", "compares", "loads", "saves"]) if __name__ == "__main__": sizes = range(1, 1001, 2) analyze_complexity(sizes)