# How to calculate the hit ratio?

29 views (last 30 days)
Sai Gautam Mandapati on 5 Aug 2022
Commented: sudobash on 8 Aug 2022
Good day everyone,
Based on the below screenshot I want to calculate the hit ratio. The formula for calculating the value would be: cachehits+cachemisses/cachehits.
For 1st second I get two types: Cachehits and Cachemisses. In this way I have the data for all the nodes in the network that I simulated. I want to know is there anyway in matlab that I can give the formula once and I can get the values for all my rows?
Please let me know. Thank you.

sudobash on 5 Aug 2022
Hello!
As per my understanding, the Hit Ratio for the entire network needs to be computed. Here is a solution:
%% Example Input
inputTable = table();
inputTable.Time = [1;1;2;2;3;3;4;4];
inputTable.Node = ["R7";"R7";"R7";"R7";"R7";"R7";"R7";"R7"];
inputTable.Type = ["CacheHits";"CacheMisses";"CacheHits";"CacheMisses";"CacheHits";"CacheMisses";"CacheHits";"CacheMisses"];
inputTable.Packets = [1;22;4;15;9;15;9;13];
inputTable
inputTable = 8×4 table
Time Node Type Packets ____ ____ _____________ _______ 1 "R7" "CacheHits" 1 1 "R7" "CacheMisses" 22 2 "R7" "CacheHits" 4 2 "R7" "CacheMisses" 15 3 "R7" "CacheHits" 9 3 "R7" "CacheMisses" 15 4 "R7" "CacheHits" 9 4 "R7" "CacheMisses" 13
%% Solution
% Find the total number of cache hits
totalCacheHits = sum(inputTable(inputTable.Type == "CacheHits",:).Packets)
totalCacheHits = 23
% Find the total number of cache misses
totalCacheMisses = sum(inputTable(inputTable.Type == "CacheMisses",:).Packets)
totalCacheMisses = 65
% Compute the Hit Ratio using the formula
hitRatio = (totalCacheMisses + totalCacheHits)/totalCacheHits
hitRatio = 3.8261
You can also find the hit ratio for each node by selecting only specific nodes like this:
R7CacheMisses = sum(inputTable(inputTable.Type == "CacheMisses" & inputTable.Node == "R7",:).Packets)
sudobash on 8 Aug 2022
Hi!
As per my understanding, I would suggest you reformat your table and find hit ratio in this way :
%% Example Input
inputTable = table();
inputTable.Time = [1;2;3;4];
inputTable.Node = ["R7";"R7";"R7";"R7"];
inputTable.Hit = [1;4;9;9]; % Hit is the number of packets of the type "CacheHit"
inputTable.Miss = [22;15;15;13]; % Miss is the number of packets of the type "CacheMiss"
inputTable
inputTable = 4×4 table
Time Node Hit Miss ____ ____ ___ ____ 1 "R7" 1 22 2 "R7" 4 15 3 "R7" 9 15 4 "R7" 9 13
inputTable.HitRatio = (inputTable.Hit + inputTable.Miss)./inputTable.Hit
inputTable = 4×5 table
Time Node Hit Miss HitRatio ____ ____ ___ ____ ________ 1 "R7" 1 22 23 2 "R7" 4 15 4.75 3 "R7" 9 15 2.6667 4 "R7" 9 13 2.4444
If you wish not to reformat, then you could use the following workaround:
%% Example Input
inputTable = table();
inputTable.Time = [1;1;2;2;3;3;4;4];
inputTable.Node = ["R7";"R7";"R7";"R7";"R7";"R7";"R7";"R7"];
inputTable.Type = ["CacheHits";"CacheMisses";"CacheHits";"CacheMisses";"CacheHits";"CacheMisses";"CacheHits";"CacheMisses"];
inputTable.Packets = [1;22;4;15;9;15;9;13];
inputTable;
for i = 1:2:height(inputTable)
inputTable.HitRatio(i) = (inputTable.Packets(i)+inputTable.Packets(i+1))/inputTable.Packets(i);
inputTable.HitRatio(i+1) = (inputTable.Packets(i)+inputTable.Packets(i+1))/inputTable.Packets(i);
end
inputTable
inputTable = 8×5 table
Time Node Type Packets HitRatio ____ ____ _____________ _______ ________ 1 "R7" "CacheHits" 1 23 1 "R7" "CacheMisses" 22 23 2 "R7" "CacheHits" 4 4.75 2 "R7" "CacheMisses" 15 4.75 3 "R7" "CacheHits" 9 2.6667 3 "R7" "CacheMisses" 15 2.6667 4 "R7" "CacheHits" 9 2.4444 4 "R7" "CacheMisses" 13 2.4444
Hope this solves your question.