- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个模拟各种缓存设计的 Java 程序。我的设计将 Cache 分为两个类,Cache 和 Set,集合的 block 表示为 Queue,以便我可以使用 LRU 算法进行替换。这是我的缓存类
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
/**
* Class to simulate a cache with a specified associativity and number of sets
*
* @author Nick Gilbert
*/
public class Cache
{
private Set[] sets;
private int setAssoc, hitCount, missCount, totalCount;
private double hitRate, missRate;
public Cache(int passedNumSets, int passedSetAssoc)
{
this.sets = new Set[passedNumSets];
for(int i = 0; i < this.sets.length; i++)
{
this.sets[i] = new Set(passedSetAssoc);
}
this.setAssoc = passedSetAssoc;
this.hitCount = 0; this.missCount = 0; this.totalCount = 0;
this.hitRate = 0.0; this.missRate = 0.0;
}
/**
* Takes a .dat file name, reads memory addresses from it, and simulates filling the cache
* as it reads each address
*/
public void fillFromFile(String fileName) throws IOException {
Scanner inFile = new Scanner(new File(fileName));
while(inFile.hasNextInt())
{
totalCount++;
int addressToRead = inFile.nextInt(); //Getting next byte address
addressToRead /= 4; //Converting to a word address
int blockAddress = addressToRead / 4;
int location = (blockAddress % sets.length); //Location = (MemoryAddress % CacheSize)
//System.out.println(blockAddress + ": set " + location);
Set setToPlaceAddress = sets[location];
boolean isHit = setToPlaceAddress.checkQueue(blockAddress);
System.out.println(totalCount + "@" + location + ": " + sets[location]);
if(isHit) {
hitCount++;
}
else {
missCount++;
}
System.out.println(isHit);
}
inFile.close();
hitRate = hitCount / (double)totalCount * 100;
missRate = missCount / (double)totalCount * 100;
}
public int getSetAssoc() {
return setAssoc;
}
public void printStats() {
System.out.println("Cache Stats!\n-----------------");
System.out.println(this);
System.out.println("Hit Count: " + hitCount);
System.out.println("Miss Count: " + missCount);
System.out.println("Hit Rate: " + hitRate);
System.out.println("Miss Rate: " + missRate);
}
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Cache Sets: " + sets.length + "\n");
sb.append("Set Associativity: " + setAssoc + "\n");
sb.append("Block Size: 4");
return sb.toString();
}
}
这是我的类(class)
import java.util.*;
/**
* Class to simulate a set in a cache
* @author Nick Gilbert
*/
public class Set {
private Queue<Integer> blocks; //Data contained in the set
private int setLength; //Set associativity
/**
* Constructor
*/
public Set(int setLength)
{
this.setLength = setLength;
blocks = new ArrayDeque<Integer>();
}
/**
* Check if the block is already there and placing it if it is not
*/
public boolean checkQueue(int blockAddress) {
if(blocks.contains(blockAddress)) { //If the queue contains the address
updateQueue(blockAddress); //Move it to the back (most recently used)
//System.out.println(blockAddress + ": hit");
return true; //It's a hit
}
insertWithLRU(blockAddress); //Insert address with LRU algorithm
//System.out.println(blockAddress + ": miss");
return false; //It's a miss
}
/**
* Method to move address to the back of the queue
*/
private void updateQueue(int mostRecent) {
Iterator<Integer> queueIterator = blocks.iterator(); //Iterator to check through the queue
while(queueIterator.hasNext()) { //Finding the matching address
int addressToCheck = queueIterator.next();
if(addressToCheck == mostRecent) { //When we've found it
queueIterator.remove(); //Remove it to be readded
break;
}
}
blocks.add(mostRecent); //Re-adding it to the back
}
/**
* Algorithm to remove the least recently used address and add a new one
*/
private void insertWithLRU(int address) {
if(blocks.size() >= setLength) { //If queue is full
blocks.remove();
//System.out.println(blocks.remove() + " removed"); //Remove the front one, the least recently used
}
blocks.add(address); //Add new one to the back
}
public String toString() {
String str = "[";
Iterator<Integer> queueIterator = blocks.iterator(); //Iterator to check through the queue
while(queueIterator.hasNext()) { //Finding the matching address
str += queueIterator.next() + ", ";
}
return str;
}
}
还有我的主课
import java.io.IOException;
import java.util.Scanner;
/**
* A program to simulate different types of caches for statistical analysis
* on how different cache setups compare to each other
*
* Nick Gilbert
*
* NOTES: Using Byte Addresses
* Take address from file, convert to a word address
* 4 words per block
* block size = 4
* byte / 4 = word
* word / 4 = block
* block % rowsInCache = location
*
* Rows = numSets
* Cols = setAssoc
*/
//TODO FIX LRU ALGORITHM
public class CacheSimulator
{
/**
* Main method
*/
public static void main(String[] args) throws IOException
{
//Creating the cache
Scanner in = new Scanner(System.in);
int numSets, setAssoc;
do
{
System.out.print("Enter number of cache sets (1/32/64/128/256/512): ");
numSets = in.nextInt();
}
while(numSets != 1 && numSets != 32 && numSets != 64 && numSets != 128 && numSets != 256 && numSets != 512);
do
{
System.out.print("Enter set associativity (1/2/4): ");
setAssoc = in.nextInt();
}
while(setAssoc != 1 && setAssoc != 2 && setAssoc != 4);
Cache cache = new Cache(numSets, setAssoc);
System.out.println("Cache created!");
//Getting file to read from
System.out.print("Enter the filename to check: ");
String datFile = in.next();
//Filling cache from file
in.close(); //End of keyboard input
cache.fillFromFile(datFile);
cache.printStats();
}
}
它似乎适用于小文件。我从 .dat 文件中读取字节地址并将它们映射到缓存。然而,当我在 this .dat file 上运行它时它应该给出 211414 的未命中计数。相反,它说未命中计数是 183099,这比预期的要少得多。我试过用小文件进行调试,它似乎工作正常,但我无法让它与这个文件一起工作。
注意:该程序确实适用于 1-way/direct-mapped Cache,因此问题似乎与 LRU 算法有关,但我不知道是什么。
最佳答案
想通了!事实证明它与LRU无关。当缓存的集合关联性增加时,它不会增加缓存中可用槽的数量。因此
this.sets = new Set[passedNumSets];
应该是
this.sets = new Set[passedNumSets / setAssoc];
关于使用 LRU 的 Java 缓存模拟给出了不准确的命中率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29808525/
题目地址:https://leetcode.com/problems/lru-cache/ 题目描述 Design and implement a data structure for Least
我使用 redis 作为数据存储而不是缓存,但是设置了 maxmemory 限制,据我了解,maxmemory 指定了 redis 可以使用的 RAM,如果达到内存限制,它不应该将数据交换回磁盘。 我
假设redis 实例中的所有键都设置了过期时间,volatile-lru 和allkeys-lru 是相似的。但是,当删除 key 时,两者之间是否存在显着的性能差异? 奖励问题: 在配置了 allk
LRU-K 是一种缓存淘汰算法,旨在改进传统的LRU(Least Recently Used,最近最少使用)算法的性能。将其中高频的数据达到K次访问移入到另一个队列进行保护。 算法思想 LR
1.题目 请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。 实现 LRUCache 类: ① LRUCache(int capacity) 以正整数作为容量 capacity
如果 memcached 中的某个项目设置为永不过期,它是否免于 LRU 驱逐? 我见过的文档并没有清楚地描绘出哪个优先。在我看来,让 LRU 仅适用于过期 > 0 的项目是理想的(可能在内部非常复杂
当 memcache 中可用内存已满时,memcache 使用 LRU(最近使用的)算法来释放内存。 我的问题是 LRU 算法是否会删除在一段时间内(最近一次使用)未使用的条目而不是过期的条目? 即将
我在java中使用LRU缓存并覆盖了removeEldest。 @Override protected boolean removeEldestEntry (Map.Entry eldest) {
我前段时间已经发帖询问LRU缓存的良好设计(C++)。您可以在此处找到问题、答案和一些代码: Better understanding the LRU algorithm 我现在尝试对这段代码进行多线
我正在尝试使用这个 youtube video 自学 LRU 算法.在下面的示例 ( taken from here ) 中,为什么 0 被 3 代替。不应该是 4 被 3 代替,因为 4 是最少使用
我正在尝试使用 Caffeine 作为 LRU 缓存,因此首先添加的条目将首先被逐出。运行这段代码: final Cache map = Caffeine.newBuilder()
我对算法LRU有一点疑问。如果您有一个包含四个 block 的缓存,您需要多少位来实现该算法? 最佳答案 假设您指的是 4 路组相联缓存: “完美”的 LRU 本质上是按照使用顺序为每一行分配一个准确
我有两种类型的 Redis 键:post:{pid} 和 comment:{cid}。 我想存储最多 100 条记录的任一类型的缓存。 例如我有 100 条帖子记录和 50 条评论记录。当评论记录到来
我正在使用 Swift 构建一个应用程序,我想在我的应用程序中使用 LRU 缓存。我实现了一个简单的 LRUCache在 Swift 中,但后来我发现,由于它已经附带了 Dictionary 和 Ar
现在软件或者网页的并发量越来越大了,大量请求直接操作数据库会对数据库造成很大的压力,处理大量连接和请求就会需要很长时间,但是实际中百分之80的数据是很少更改的,这样就可以引入缓存来进行读取,减少数据
我有一些看起来像这样的代码: from functools import lru_cache @lru_cache() def get_cheese(type): print('{}? We\
我有一个规范,我试图定义一个 LRU 缓存系统,我遇到的一个问题是如何从结构键/值对(基本上是字典或哈希映射)中删除值其他语言)。 到目前为止,这是规范本身(不完整): EXTENDS Integer
如何使用 Erlang 实现 LRU 缓存? LRU Cache Wiki 最受关注的 Github 项目是 fogfish/cache ,但分段表不太适合我的数据。 barrel-db/erlang
我正在制作一个 Android 应用程序,其中有带有缩略图的新闻文章。这些缩略图从网络加载并存储在 LruCache 中,其中 URL 作为键,位图作为值。 private LruCache tCac
这个问题已经有答案了: How would you implement an LRU cache in Java? (21 个回答) 已关闭 5 年前。 我想仅使用 Java 内置的数据结构在 Jav
我是一名优秀的程序员,十分优秀!