- 實作Cache replacement
- FIFO
- LRU
- SETUP
- Environment:WIN10
- IDE:Pycharm 4.0 Python 3.7
- Language:Python
Initial:
method='LRU'
entrysize=4Eample :
E4 2C
E2 2C
E4 2C
E3 2C
E5 2C
E4 2C
E2 2C
E2 4C
E4 2C
E8 2C
E2 2C
E2 4C
E4 2C
E8 2C
Method: LRU Entry: 4
----------------------------Round1----------------------------
[['2C', None, None, None, None], ['4C', None, None, None, None]]
Index=2C,Tag=E4,Miss
----------------------------Round14----------------------------
[['2C', 'E2', 'E8', 'E2', 'E4'], ['4C', 'E2', None, None, None]]
Index=2C,Tag=E8,Hit
[['2C', 'E2', 'E2', 'E4', 'E8'], ['4C', 'E2', None, None, None]]
Hit Rate=0.571429
Tag Index
E2 2C
E2 4C
E4 2C
E8 2C
cache=[[None for i in range(entrysize+1)] for j in range(indexnum(index))] #Crate cache table
| Index | Entry1 | Entry2 | Entry3 | Entry4 |
|---|---|---|---|---|
| Index1 | ||||
| Index2 |
4.Replace function會將Entry由右往左移並將新資料放置最後一格,最左邊則是最舊的資料,Setdata function則是會先判斷是否hit,如果hit回傳hit而LRU method則會在hit的同時把hit的資料放置最右邊
def dataReplace(index,tag):
def setdata(index,tag):
for i in range(len(tag)):
----------------------------Round14----------------------------
[['2C', 'E2', 'E8', 'E2', 'E4'], ['4C', 'E2', None, None, None]]
Index=2C,Tag=E8,Hit
[['2C', 'E2', 'E2', 'E4', 'E8'], ['4C', 'E2', None, None, None]]
Hit Rate=0.571429