Last active
December 6, 2021 01:29
-
-
Save hamzahkhan/cc9efb426ad73a9c682e8d2661b4d0f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
int predict(vector<int> v, vector<int> &fr, int pn, int index) | |
{ | |
int res = -1, farthest = index; | |
for (int i = 0; i < fr.size(); i++) | |
{ | |
int j; | |
for (j = index; j < pn; j++) | |
{ | |
if (fr[i] == v[j]) | |
{ | |
if (j > farthest) | |
{ | |
farthest = j; | |
res = i; | |
} | |
break; | |
} | |
} | |
if (j == pn) | |
return i; | |
} | |
return (res == -1) ? 0 : res; | |
} | |
bool search(int key, vector<int> &fr) | |
{ | |
for (int i = 0; i < fr.size(); i++) | |
if (fr[i] == key) | |
return true; | |
return false; | |
} | |
int opr(vector<int> v, int pn, int fn) | |
{ | |
vector<int> fr; | |
int hit = 0; | |
for (int i = 0; i < pn; i++) | |
{ | |
if (search(v[i], fr)) | |
{ | |
hit++; | |
continue; | |
} | |
if (fr.size() < fn) | |
fr.push_back(v[i]); | |
else | |
{ | |
int j = predict(v, fr, pn, i + 1); | |
fr[j] = v[i]; | |
} | |
} | |
cout << "Hits = " << hit << endl; | |
cout << "Misses = " << pn - hit << endl; | |
return (pn - hit); | |
} | |
int main(int argc, char **argv) | |
{ | |
if (argc != 3) | |
{ | |
cout << "Give Proper Argument" << endl; | |
return 0; | |
} | |
vector<int> v; | |
int n = stoi(argv[1]); | |
int k = stoi(argv[2]); | |
cout << n; | |
for (int i = 0; i < n; i++) | |
v.push_back(rand() % k); | |
for (int i = 0; i < n; i++) | |
cout << v[i] << " "; | |
cout << endl; | |
std::ofstream myfile; | |
myfile.open("example.csv"); | |
myfile << "frame,miss,\n"; | |
int fn = 3; | |
for (int i = 4; i <= k; i++) | |
{ | |
int pn = sizeof(v) / sizeof(v[0]); | |
cout << "frame size :" << i << endl; | |
int miss = opr(v, pn, i); | |
myfile << i << "," << miss << ",\n"; | |
cout << "-----------------------------" << endl; | |
} | |
myfile.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment