Cache Hits in Memory Organization
Last Updated :
01 May, 2022
The user has a memory machine. It has one layer for data storage and another layer for the cache. The user has stored an array with length N in the first layer. When the CPU needs data, it immediately checks in cache memory whether it has data or not. If data is present it results in CACHE HITS, else CACHE MISS, i.e., data is not in cache memory so it retrieves data from main memory and inserts a block of data into the cache layer.

The question that arises is: How many times machine will need to load a block into the cache layer, i.e. determine the number of CACHE MISS?
Example
Let us assume an array and denote its elements by A0, A1, …, AN? Now user wants to load some elements of this array into the cache.

Machine loads array in blocks with size B:
Assume block size is 4.
1 2 3 4 comes in Block 1, 5 6 7 8 comes in Block 2 and 9,10 comes in Block 3.

A0, A1, …, AB? 1 form a block; AB, AB+1, … , A2B? 1 form another block, and so on. The last block may contain less than B elements of user’s array.
Cache may only contain at most one block at a time. Whenever user tries to access an element Ai, machine checks if block where Ai is located is already in cache, and if it is not, loads this block into cache layer, so that it can quickly access any data in it.
However, as soon as user tries to access an element that is outside currently loaded block in cache, block that was previously loaded into cache is removed from cache, since machine loads a new block containing element that is being accessed.
Example –
User has a sequence of elements Ax1, Ax2, …, AxM which he wants to access, in this order. Initially, cache is empty. We need to find out how many times machine will need to load a block into cache layer.
Input Format :
- First line of each test case contains three space-separated integers N, B and M.
- Second line contains M space-separated integers x1, x2, …, xM.
Input :
5 3 3
0 3 4
Output :
2
Explanation :
Machine stores elements [A0, A1, A2] in one block and [A3, A4] in another block. When accessing A0, block [A0, A1, A2] is loaded. Then, accessing A3 removes previous block from cache and loads block [A3, A4]. Finally, when user accesses A4, a new block is not loaded, since block containing A4 is currently loaded in cache.
Approach :
- Initially cache miss occurs because cache layer is empty and we find next multiplier and starting element.
- Obtain user value and find next multiplier number which is divisible by block size.
- Find starting elements of current block.
- If user value is greater than next multiplier and lesser than starting element then cache miss occurs.
Implementation :
C++
#include <iostream>
using namespace std;
int findnextmultiplier(int i, int b)
{
for (int j = i; j <= i * b; j++) {
if (j % b == 0)
return j;
}
}
int ans(int n, int b, int m, int user[])
{
int start, cacheMiss = 1, nextmultiplier;
nextmultiplier= findnextmultiplier(user[0] + 1, b);
start = nextmultiplier - b + 1;
for (int i = 1; i < m; i++) {
if (user[i] + 1 > nextmultiplier || user[i] + 1 < start) {
cacheMiss++;
nextmultiplier= findnextmultiplier(user[i] + 1, b);
start = nextmultiplier - b + 1;
}
}
cout << cacheMiss << endl;
return 0;
}
int main()
{
int n=5, b=3, m=3;
int user[3] = {0, 3, 4};
ans(n, b, m, user);
return 0;
}
|
Java
public class Main
{
public static int findnextmultiplier(int i, int b)
{
for (int j = i; j <= i * b; j++)
{
if (j % b == 0)
return j;
}
return 0;
}
public static int ans(int n, int b, int m, int user[])
{
int start, ch = 1, nextmultiplier;
nextmultiplier= findnextmultiplier(user[0] + 1, b);
start = nextmultiplier - b + 1;
for (int i = 1; i < m; i++)
{
if (user[i] + 1 > nextmultiplier || user[i] + 1 < start)
{
ch++;
nextmultiplier= findnextmultiplier(user[i] + 1, b);
start = nextmultiplier - b + 1;
}
}
System.out.println(ch);
return 0;
}
public static void main(String[] args)
{
int n=5, b=3, m=3;
int user[] = {0, 3, 4};
ans(n, b, m, user);
}
}
|
Python3
def findnextmultiplier(i, b):
for j in range(i, (i * b) + 1):
if (j % b == 0):
return j
def ans(n, b, m, user):
ch = 1
nextmultiplier = findnextmultiplier(user[0] + 1, b)
start = nextmultiplier - b + 1
for i in range(1, m):
if (user[i] + 1 > nextmultiplier or
user[i] + 1 < start):
ch += 1
nextmultiplier = findnextmultiplier(
user[i] + 1, b)
start = nextmultiplier - b + 1
print(ch)
n = 5
b = 3
m = 3
user = [ 0, 3, 4 ]
ans(n, b, m, user)
|
C#
using System;
class GFG{
static int findnextmultiplier(int i, int b)
{
for(int j = i; j <= i * b; j++)
{
if (j % b == 0)
return j;
}
return 0;
}
static int ans(int n, int b, int m, int[] user)
{
int start, ch = 1, nextmultiplier;
nextmultiplier = findnextmultiplier(
user[0] + 1, b);
start = nextmultiplier - b + 1;
for(int i = 1; i < m; i++)
{
if (user[i] + 1 > nextmultiplier ||
user[i] + 1 < start)
{
ch++;
nextmultiplier = findnextmultiplier(
user[i] + 1, b);
start = nextmultiplier - b + 1;
}
}
Console.WriteLine(ch);
return 0;
}
static void Main()
{
int n = 5, b = 3, m = 3;
int[] user = { 0, 3, 4 };
ans(n, b, m, user);
}
}
|
Javascript
<script>
function findnextmultiplier(i, b)
{
for(let j = i; j <= i * b; j++)
{
if (j % b == 0)
return j;
}
return 0;
}
function ans(n, b, m, user)
{
let start, ch = 1, nextmultiplier;
nextmultiplier =
findnextmultiplier(user[0] + 1, b);
start = nextmultiplier - b + 1;
for(let i = 1; i < m; i++)
{
if (user[i] + 1 > nextmultiplier
|| user[i] + 1 < start)
{
ch++;
nextmultiplier =
findnextmultiplier(user[i] + 1, b);
start = nextmultiplier - b + 1;
}
}
document.write(ch + "</br>");
return 0;
}
let n = 5, b = 3, m = 3;
let user = [ 0, 3, 4 ];
ans(n, b, m, user);
</script>
|
Output :
2
Time Complexity : O(m)
Auxiliary Space : O(1)
Similar Reads
How to Convert a String to ArrayList in Java?
Converting String to ArrayList means each character of the string is added as a separator character element in the ArrayList. Example: Input: 0001 Output: 0 0 0 1 Input: Geeks Output: G e e k s We can easily convert String to ArrayList in Java using the split() method and regular expression. Paramet
1 min read
Scala SortedSet mkString() method with a separator with example
The mkString() method is utilized to display all the elements of the SortedSet in a string along with a separator. Method Definition: def mkString(sep: String): String Return Type: It returns all the elements of the SortedSet in a string along with a separator. Example #1: // Scala program of mkStri
1 min read
Scala Set mkString() method with a separator with example
The mkString() method is utilized to display all the elements of the set in a string along with a separator. Method Definition: def mkString(sep: String): String Return Type: It returns all the elements of the set in a string along with a separator. Example #1: // Scala program of mkString() // meth
1 min read
Scala Stack mkString() method with a separator with example
In Scala Stack class, the mkString() method is utilized to display all the elements of the stack in a string along with a separator. Method Definition: def mkString(sep: String): String Return Type: It returns all the elements of the stack in a string along with a separator. Example #1: // Scala pro
1 min read
Scala List mkString() method with a separator with example
The mkString() method is utilized to display all the elements of the list in a string along with a separator. Method Definition: def mkString(sep: String): String Return Type: It returns all the elements of the list in a string along with a separator. Example #1: // Scala program of mkString() // me
1 min read
Scala TreeSet mkString() method with a separator with example
The mkString() method is utilized to display all the elements of the TreeSet in a string along with a separator. Method Definition: def mkString(sep: String): String Return Type: It returns all the elements of the TreeSet in a string along with a separator. Example #1: // Scala program of mkString()
1 min read
Scala Queue mkString() method with a separator with example
The mkString() method is utilized to display all the elements of the queue in a string along with a separator. Method Definition: def mkString(sep: String): String Return Type: It returns all the elements of the queue in a string along with a separator. Example #1: // Scala program of mkString() //
1 min read
Scala immutable TreeSet mkString() method with a separator
In Scala immutable TreeSet class the mkString() method is utilised to display all the elements of the TreeSet in a string along with a separator. Method Definition: def mkString(sep: String): String Return Type: It returns all the elements of the TreeSet in a string along with a separator. Example #
1 min read
Scala List mkString() method with a start, a separator and an end with example
This method is same as mkString() method but here a start, a separator and an end is also included. Method Definition: defmkString(start: String, sep: String, end: String): String Return Type: It returns the elements of the list as a string along with a stated start, separator and an end. Let's chec
1 min read
Scala Iterator mkString() method with a separator with example
This method is same as mkString() method but here a separator is included. Method Definition: def mkString(sep: String): String Return Type: It returns the string representation of the stated collection with a given separator. Example #1: // Scala program of mkString() // method with a separator //
1 min read
Scala Iterator mkString() method with a start, a separator and an end with example
This method is also same as mkString() method but here it is accompanied by a start, a separator and an end. Method Definition: def mkString(start: String, sep: String, end: String): String Where, start is the starting string, sep is the separator string and end is the ending string. Return Type: It
1 min read
Scala Map mkString() method with a start, a separator and an end with example
This method is same as mkString() but here a start, a separator, and an end is also added. Method Definition: defmkString(start: String, sep: String, end: String): String Return Type: It returns the elements of the map as a string along with a stated start, separator and an end. Example #1: // Scala
1 min read
Scala Map mkString() method with a separator with example
This method is same as mkString() method but here a separator is also added. Method Definition: def mkString(sep: String): String Return Type: It returns the elements of the map as a string along with the separator between them. Example #1: // Scala program of mkString() // method with a separator /
1 min read
Scala SortedSet mkString() method with example
The mkString() method is utilized to display all elements of the SortedSet in a string. Method Definition: def mkString: String Return Type: It returns a string containing all the element of the SortedSet. Example #1: // Scala program of mkString() // method import scala.collection.immutable.SortedS
1 min read
Scala Set mkString() method with example
The mkString() method is utilized to display all elements of the set in a string. Method Definition: def mkString: String Return Type: It returns a string containing all the element of the set. Example #1: // Scala program of mkString() // method // Creating object object GfG { // Main method def ma
1 min read
Scala List addString() method with a separator with example
This method is same as addString() method but here a separator is also included. Method Definition: def addString(b: StringBuilder, sep: String): StringBuilder Return Type: It returns the elements of the list to a string builder along with a separator between them. Example #1: // Scala program of ad
1 min read
Scala Stack mkString() method with example
In Scala Stack class, the mkString() method is utilized to display all elements of the stack in a string. Method Definition: def mkString: String Return Type: It returns a string containing all the element of the stack. Example #1: // Scala program of mkString() // method // Import Stack import scal
1 min read
Scala Map mkString() method with example
The mkString() method is utilized to represent the elements of the map as a string. Method Definition: def mkString: String Return Type: It returns the elements of the map as a string. Example #1: // Scala program of mkString() // method // Creating object object GfG { // Main method def main(args:A
1 min read
Scala SortedMap mkString() method with a start, a separator and an end with example
This method is same as mkString() but here a start, a separator, and an end is also added. Method Definition: defmkString(start: String, sep: String, end: String): String Return Type: It returns the elements of the SortedMap as a string along with a stated start, separator and an end. Example #1: //
1 min read