H. A Cache Simulator
Cache memories have been used widely in current microprocessor systems. In this problem, you are asked to write a program for a cache simulator. The cache has the following metrics:
1. The cache size is 1 KB (K-byte).
2. The cache uses the direct mapped approach.
3. The cache line size is 16 bytes.
4. The cacheable memory size is 256MB.
Your program will report a hit or miss when an address is given to the cache simulator. This is often called trace simulation. Initially, all of the cache lines are in the invalid state. When a memory line is first brought into the cache, the allocated cache entry transits into the valid state. Assume that a miss causes the filling of a whole cache line.
Input Format
Up to 100100 lines of address can be given in the file. Each line consists of an address given to the simulator. The address is given in hexadecimal form. We assume that each address references only one byte of data. The input file ends at the line with the word ENDEND.
Output Format
Report either HitHit or MissMiss for each of the given addresses. The last line reports cache hit ratio in percentage, that is, the number of hits divided by the number of total addresses given.
样例输入
AAAA000
00010B2
00010BA
END
样例输出
Miss
Miss
Hit
Hit ratio = 33.33%
题目来源
题意:
模拟一个Cache, Cache的大小是1KB,每一块大小是16B,一共64块,采用direct mapped方式,主存大小是256MB;
思路:
因为计组成原理还没学到这,就现场学习了一下,有关direct mapped方式可以参考https://wenku.baidu.com/view/7ea3d85e10661ed9ad51f3f3.html?from=search,只要理解了direct mapped方式之后这题就好写了,用公式建立主存和Cache之间的联系,Cache一共64块,公式就是I = (address / 16)% 64;
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<map> using namespace std; const int M = + ; map<int, string> mp;
char ch[M]; int main(){
int total = , hit = ;
while(scanf("%s", ch) == && strcmp(ch, "END")){
int sum = ;
sscanf(ch, "%x", &sum);
total ++;
int I = (sum / ) % ;
ch[] = '\0';
if(mp[I] == ch){
hit ++;
printf("Hit\n");
}else{
mp[I] = ch;
printf("Miss\n");
}
}
printf("Hit ratio = %.2f%%\n",double(hit*100.0/ total));
}
H. A Cache Simulator的更多相关文章
- UVA 11423 - Cache Simulator (树状数组)
UVA 11423 - Cache Simulator (树状数组) option=com_onlinejudge&Itemid=8&category=523&page=sho ...
- 2017 icpc 南宁网络赛
2000年台湾大专题...英语阅读输入输出专场..我只能说很强势.. M. Frequent Subsets Problem The frequent subset problem is define ...
- CSAPP-Lab05 Cache Lab 深入解析
本文首发于我的知乎专栏:https://zhuanlan.zhihu.com/p/484657229 实验概览 Cache Lab 分为两部分,编写一个高速缓存模拟器以及要求优化矩阵转置的核心函数,以 ...
- 【CSAPP】Cache Lab 实验笔记
cachelab这节先让你实现个高速缓存模拟器,再在此基础上对矩阵转置函数进行优化,降低高速缓存不命中次数.我的感受如上一节,实在是不想研究这些犄角旮旯的优化策略了. 前期准备 我实验的时候用到了va ...
- Cache的原理、设计及实现
Cache的原理.设计及实现 前言 虽然CPU主频的提升会带动系统性能的改善,但系统性能的提高不仅仅取决于CPU,还与系统架构.指令结构.信息在各个部件之间的传送速度及存储部件的存取速度等因素有关,特 ...
- [z]计算机架构中Cache的原理、设计及实现
前言 虽然CPU主频的提升会带动系统性能的改善,但系统性能的提高不仅仅取决于CPU,还与系统架构.指令结构.信息在各个部件之间的传送速度及存储部件的存取速度等因素有关,特别是与CPU/内存之间的存取速 ...
- Linux内核中内存cache的实现【转】
Linux内核中内存cache的实现 转自:http://blog.chinaunix.net/uid-127037-id-2919545.html 本文档的Copyleft归yfydz所有,使用 ...
- Method and apparatus for verification of coherence for shared cache components in a system verification environment
A method and apparatus for verification of coherence for shared cache components in a system verific ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
随机推荐
- 利用freemarker生成word,word另存为xml文件的标签解析
http://wenku.baidu.com/link?url=YxTZWVP3ssO-e_Br3LIZVq2xOQnqaSz8gLPiRUDN8NIR_wX2-Z25OqwbVn5kXqGiOFYU ...
- JavaBean,EJB,POJO,Spring Bean 的演进历程
JavaBean Sun公司对类提出的规范:1,类是public的2,有一个无参构造方法3,属性修饰要用private,通过get set操作4,实现Serializable接口5,对事件使用Swin ...
- 从零开始学习vue(2)
一.vue实例 每个vue应用都是通过Vue构造函数创建的一个新的实例开始的: var vm = new Vue({ //选项对象 }) 在这其中vm(viewModel的简称)通常都表示vue实例的 ...
- Mysql : Maximum execution time of 30 seconds exceeded
在向Mysql数据库中插入数据时,提示Maximum execution time of 30 seconds exceeded.......翻译:最大运行时间超过30秒. 最后在php.ini中找到 ...
- legend3---lavarel常用操作代码
legend3---lavarel常用操作代码 一.总结 一句话总结: 要自己总结一下常用代码,这样才方便,也才有收获 1.路由示例:Route::get('/login','Home\Login\L ...
- spark MLlib 概念 5: 余弦相似度(Cosine similarity)
概述: 余弦相似度 是对两个向量相似度的描述,表现为两个向量的夹角的余弦值.当方向相同时(调度为0),余弦值为1,标识强相关:当相互垂直时(在线性代数里,两个维度垂直意味着他们相互独立),余弦值为0, ...
- MySQL查看数据表的创建时间和最后修改时间
如何MySQL中一个数据表的创建时间和最后修改时间呢? 可以通过查询information_schema.TABLES 表得到信息. 例如 mysql> SELECT * FROM `infor ...
- 安装源配置文件“/etc/apt/sources.list”问题
安装docker过程中使用以下命令设置稳定存储库. $ sudo add-apt-repository \ "deb [arch=amd64] https://download.docker ...
- cocoapods [!] Unable to find a pod with name, author, summary, or description matching `xx`
pod search MJRefresh的时候报错 [!] Unable to find a pod with name, author, summary, or description matchi ...
- Python基本语法_输入/输出语句详解
目录 目录 前言 输入 raw_input input raw_input 和 input 的区别 输出 print print 基本格式化输出 print复杂格式化输出 flags标志位 width ...