How to calculate the hit ratio?
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
sudobash
2022-8-5
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 个评论
sudobash
2022-8-8
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!