How to calculate the hit ratio?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Antworten (1)
sudobash
am 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
%% Solution
% Find the total number of cache hits
totalCacheHits = sum(inputTable(inputTable.Type == "CacheHits",:).Packets)
% Find the total number of cache misses
totalCacheMisses = sum(inputTable(inputTable.Type == "CacheMisses",:).Packets)
% Compute the Hit Ratio using the formula
hitRatio = (totalCacheMisses + totalCacheHits)/totalCacheHits
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)
Hope this addresses your question.
4 Kommentare
sudobash
am 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.HitRatio = (inputTable.Hit + inputTable.Miss)./inputTable.Hit
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
Hope this solves your question.
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!