Skip to content

Instantly share code, notes, and snippets.

@sushant-hiray
Created October 15, 2013 06:07
Show Gist options
  • Save sushant-hiray/6987208 to your computer and use it in GitHub Desktop.
Save sushant-hiray/6987208 to your computer and use it in GitHub Desktop.
Programmatically calculating Cache Line Size
#include<stdlib.h>
#include<stdio.h>
#include<sys/time.h>
#include<iostream>
#include <ctime>
#include <cstdio>
#include <fstream>
#include<istream>
using namespace std;
void clflush(volatile char *p){
asm volatile ("clflush (%0)" :: "r"(p));
}
int main(){
ofstream myfile;
myfile.open("data.txt");
srand (time(NULL));
struct timeval tm;
int big_size= 5000000;
int small_size = 500000;
int j =0;
char t;
int step_size = 64;
long start,end,accumulate=0,accumulate1=0;
int ind;
volatile char* a = new char[big_size];
for(int i =0;i<big_size;i++){
a[i] =1;
}
step_size =1;
int count[100];
int b=0;
for(int hjk=0;hjk<100;hjk++)
count[hjk]=0;
for(int i=0;i<10000;i++){
b=0;
step_size =1;
while(step_size < 4096){
gettimeofday(&tm,NULL);
start = tm.tv_sec*1000000 + tm.tv_usec;
t=a[step_size];
gettimeofday(&tm,NULL);
end = tm.tv_sec*1000000 + tm.tv_usec;
clflush(&a[step_size]);
count[b++] += end - start;
step_size *=2;
}
}
step_size=1;
b=0;
while(step_size < 4096){
cout<<step_size<<' '<<count[b]<<endl;
step_size *=2;
b++;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment