Miscellaneous scripts
This repository contains miscellaneous scripts that does not fit in one repository, yet I will use them sometimes for my personal use. Note that some of the scripts might contain hardcoded paths and opinionated presets, and you are advised to inspect them before actually using.
Loading...
Searching...
No Matches
cache_memory.py
Go to the documentation of this file.
1def add_cache(cache: list[dict], block: int, history: int, miss: int) -> tuple:
2 address = block % 4
3 if cache[address].get(block):
4 cache[address][block] = history
5 return cache, miss
6 if len(cache[address]) < 2:
7 cache[address][block] = history
8 miss += 1
9 return cache, miss
10 assert len(cache[address]) < 3
11 for data in cache[address]:
12 # least recently used
13 if cache[address][data] == min(cache[address].values()):
14 del cache[address][data]
15 cache[address][block] = history
16 miss += 1
17 return cache, miss
18
19
20def direct_map_add_cache(cache: list[dict], block: int, history: int, miss: int) -> tuple:
21 address = block % 8
22 if cache[address].get(block):
23 cache[address][block] = history
24 return cache, miss
25 else:
26 cache[address] = {block: history}
27 miss += 1
28 return cache, miss
29
30
31counter = 1
32miss = 0
33cache = [{}, {}, {}, {}, {}, {}, {}, {}]
34for i in ['1', '2', '3', '5', '6', '2', '3', '4', '9', '10', '11', '6', '3', '6', '1', '7', '8', '4', '5', '9', '11',
35 '1', '2', '4', '5', '12', '13', '14', '15', '13', '14']:
36 cache, miss = direct_map_add_cache(cache, int(i), counter, miss)
37 counter += 1
38 print(i, cache, miss)
39
40counter = 1
41miss = 0
42cache = [{}, {}, {}, {}]
43for i in ['1', '2', '3', '5', '6', '2', '3', '4', '9', '10', '11', '6', '3', '6', '1', '7', '8', '4', '5', '9', '11',
44 '1', '2', '4', '5', '12', '13', '14', '15', '13', '14']:
45 cache, miss = add_cache(cache, int(i), counter, miss)
46 counter += 1
47 print(i, cache, miss)
tuple add_cache(list[dict] cache, int block, int history, int miss)
tuple direct_map_add_cache(list[dict] cache, int block, int history, int miss)