首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算CPU缓存命中次数

计算CPU缓存命中次数
EN

Stack Overflow用户
提问于 2020-11-21 17:14:06
回答 1查看 83关注 0票数 1

我很难理解计算缓存命中的过程/公式是什么。所以,如果我们有一个主内存,有16个条目,缓存内存有4个条目,CPU加载内存地址: 0,1,2,8,9,2‘,如果缓存是直接映射的,b)双向关联的,我如何计算命中次数a?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-30 13:38:31

假设没有预取器和LRU作为替换机制。

a)用于直接映射缓存,每个内存项只能在一个缓存项中.缓存将像这样映射(默认假定分布一致):

代码语言:javascript
复制
Cache 0 --> can hold 0,4,8,12 of the main memory entries.
Cache 1 --> can hold 1,5,9,13 of the main memory entries.
Cache 2 --> can hold 2,6,10,14 of the main memory entries.
Cache 3 --> can hold 3,7,11,15 of the main memory entries.

重置后,缓存为空。

代码语言:javascript
复制
Load from 0 will be missed and will be cached in cache entry 0.
Load from 1 will be missed and will be cached in cache entry 1.
Load from 2 will be missed and will be cached in cache entry 2.
Load from 8 will be missed and will be cached in cache entry 0 (replaced load 0).
Load from 9 will be missed and will be cached in cache entry 1 (replaced load 1).
load from 2 will be hit and will be taken from cache entry 2.

:1次命中,5次未命中,命中率为1/(5+1) = 1/6 = 16%

b)用于2种方式的关联,您将对缓存中的内存中的每个条目有两个要访问的条目。因此,set0 (缓存中的条目0,1 )将保存所有偶数主内存条目,而set1将保存所有奇怪的条目,因此,如果我们跨越它,就会有如下所示:

代码语言:javascript
复制
cache 0 (set 0) --> can hold 0,2,4,6,8,10,12,14 of the main memory entries.
cache 1 (set 0) --> can hold 0,2,4,6,8,10,12,14 of the main memory entries.
cache 2 (set 1) --> can hold 1,3,5,7,9,11,13,15 of the main memory entries.
cache 2 (set 0) --> can hold 1,3,5,7,9,11,13,15 of the main memory entries.

重置后,缓存为空。

代码语言:javascript
复制
Load from 0 will be missed and will be cached in cache entry 0.
Load from 1 will be missed and will be cached in cache entry 2.
Load from 2 will be missed and will be cached in cache entry 1.
Load from 8 will be missed and will be cached in cache entry 0 (replaced load 0 because we replace the Least Recently Used).
Load from 9 will be missed and will be cached in cache entry 3.
load from 2 will be hit and will be taken from cache entry 1.

在这种情况下,命中率是相同的:1/(5+1) = 1/6 = 16%

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64945848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档