- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试编写一个模拟 LRU 页面替换的函数。我非常了解 LRU,但在编码时遇到问题。以下内容被传递到 LRU 函数中。用户指定#'s 1-9 的 20 个字符引用字符串,它存储在一个名为 refString 的大小为 20 的数组中。用户输入的帧数 (1-7) 存储在变量 numFrames 中。最后传入一个大小为7的数组frame。
这是我的代码,我得到了一个接近但不完全的数字。也许有人可以帮忙!
private static void LRU(int numFrames, int[] refString, int[] frame)
{
int i, j = 0, k, m, flag = 0, count = 0, top = 0;
for (i = 0; i < 20; i++)
{
for (k = 0; k < numFrames; k++)
{
if (frame[k] == refString[i])
{
flag = 1;
break;
}
}
if (j != numFrames && flag != 1)
{
frame[top] = refString[i];
j++;
if (j != numFrames)
{
top++;
}
}
else
{
if (flag != 1)
{
for (k = 0; k < top; k++)
{
frame[k] = frame[k + 1];
}
frame[top] = refString[i];
}
if (flag == 1)
{
for (m = k; m < top; m++)
{
frame[m] = frame[m + 1];
}
frame[top] = refString[i];
}
}
if (flag == 0)
{
count++;
}
else
{
flag = 0;
}
}
Console.WriteLine("\nThe number of page faults with LRU is: " + count);
}
最佳答案
您的代码中几乎没有错误:-
if (top < numFrames)
{
frame[top++] = refString[i];
fault++;
}
在这里你永远不会检查当前的 refString[i] 是否已经在 frame[] 中,因为在那种情况下你不会出错并且不应该将它添加到 frame 中。
这是一个伪代码,可以帮助您消除疑虑:-
void LRU(int numframes,int refString[],int frames[]) {
int top = 0,fault=0;
int* count = new int[numframes];
for(int i=0;i<refString.length;i++) {
int k = findmax(refString[i],frames,count,top,numframes);
if(k<0) {
count[top] = 0;
frames[top++] = refString[i];
fault++;
}
else if(frames[k]!=refString[i]) {
count[k] = 0;
frames[k] = refString[i];
fault++;
}
else count[k] = 0;
for(int j=0;j<top;j++) {
count[j]++;
}
}
return(fault);
}
int findmax(int keyframe,int frames[],int count,int top,int numframes) {
int max = 0;
for(int i=0;i<top;i++) {
if(frames[i]==keyframe) {
return(i);
}
if(count[max]<count[i])
max = i;
}
if(top<numframes)
return(-1);
return(max);
}
编辑:
伪代码解释:-
1. check if current requested frame is in cache and if yes then get its index
2. if frame is present then set its count to zero so as to indicate it is used very recently, higher the count the more least recently frame is used.
3. if frame is not present in cache then
a. check if cache is full if not add new frame to end of cache and increment the fault and top of cache
b. else if chace is full then get frame with maximum count(LRU frame) and replace it with new frame and reset count to zero and increment fault.
4. increment all the counts
5. do 1 to 4 till end of all requests
6. output the no of faults
关于c# - LRU页面置换算法C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20136696/
题目地址: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
我是一名优秀的程序员,十分优秀!