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%

题目来源

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛

题意:

模拟一个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的更多相关文章

  1. UVA 11423 - Cache Simulator (树状数组)

    UVA 11423 - Cache Simulator (树状数组) option=com_onlinejudge&Itemid=8&category=523&page=sho ...

  2. 2017 icpc 南宁网络赛

    2000年台湾大专题...英语阅读输入输出专场..我只能说很强势.. M. Frequent Subsets Problem The frequent subset problem is define ...

  3. CSAPP-Lab05 Cache Lab 深入解析

    本文首发于我的知乎专栏:https://zhuanlan.zhihu.com/p/484657229 实验概览 Cache Lab 分为两部分,编写一个高速缓存模拟器以及要求优化矩阵转置的核心函数,以 ...

  4. 【CSAPP】Cache Lab 实验笔记

    cachelab这节先让你实现个高速缓存模拟器,再在此基础上对矩阵转置函数进行优化,降低高速缓存不命中次数.我的感受如上一节,实在是不想研究这些犄角旮旯的优化策略了. 前期准备 我实验的时候用到了va ...

  5. Cache的原理、设计及实现

    Cache的原理.设计及实现 前言 虽然CPU主频的提升会带动系统性能的改善,但系统性能的提高不仅仅取决于CPU,还与系统架构.指令结构.信息在各个部件之间的传送速度及存储部件的存取速度等因素有关,特 ...

  6. [z]计算机架构中Cache的原理、设计及实现

    前言 虽然CPU主频的提升会带动系统性能的改善,但系统性能的提高不仅仅取决于CPU,还与系统架构.指令结构.信息在各个部件之间的传送速度及存储部件的存取速度等因素有关,特别是与CPU/内存之间的存取速 ...

  7. Linux内核中内存cache的实现【转】

    Linux内核中内存cache的实现 转自:http://blog.chinaunix.net/uid-127037-id-2919545.html   本文档的Copyleft归yfydz所有,使用 ...

  8. 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 ...

  9. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

随机推荐

  1. ZeroMQ+QT 字符串收发

    结合 Zeromq API函数 与 Qt 字符串QString QByteArray 实现字串收发: 发送端: zmq_msg_t msg; QString strT = “ABC汉字123”: QB ...

  2. 「HEOI 2016/TJOI 2016」求和

    题目链接 戳我 \(Solution\) 先化简式子: \[f(n)=\sum_{i=0}^n\sum_{j=0}^i\begin{Bmatrix} i \\ j \end {Bmatrix}*2^j ...

  3. Java程序,JVM之间的关系

    java程序是跑在JVM上的,严格来讲,是跑在JVM实例上的.一个JVM实例其实就是JVM跑起来的进程,二者合起来称之为一个JAVA进程.各个JVM实例之间是相互隔离的. 每个java程序都运行于某个 ...

  4. Java多线程深度探索

    线程 1.优先级 每个线程有一个优先级,更高优先级的线程优先运行,优先的意思是只是在分配cpu时间段的时候,得到的概率高一些.当在某个线程创建一个新的线程,这个线程有与创建线程相同的优先级.setPr ...

  5. 【学习】windows 下PostgreSQL导入sql文件

    在软件的安装目录的bin文件下打开命令行工具 输入: psql -d 数据库名称 -h 数据库地址 -p  5432  -U  postgres  -f  sql文件(E:\Config\SQL\iS ...

  6. Scala学习(一)——基础语法

    Scala语言是一种面向对象语言,结合了命令式(imperative)和函数式(functional)编程风格,其设计理念是创造一种更好地支持组件的语言. 特性 多范式(Multi-Paradigm) ...

  7. 有关sublime的一些使用

    习惯了vs的快捷键,用sublime的时候始终感觉不太跟手. 点击 “首选项”→“按键绑定-默认” 在新的界面中就可以更改你想要的快捷键了. 1.自动格式化对齐: 查找 "command&q ...

  8. shell脚本获取绝对路径

    当前脚本全路径 echo $(readlink -f "$0") 获取绝对路径(不带文件名) echo $(dirname $(readlink -f "$0" ...

  9. vue的周期函数

    beforeCreate(创建前) created(创建后) beforeMount(载入前) mounted(载入后) beforeUpdate(更新前) updated(更新后) beforeDe ...

  10. 转载 Golang []byte与string转换的一个误区

    Golang []byte与string转换的一个误区 https://www.oyohyee.com/post/Note/golang_byte_to_string/ 2019-08-10 23:4 ...