Skip to content

Instantly share code, notes, and snippets.

@t6847kimo
Created August 18, 2019 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t6847kimo/c3ed7464a903eb1b3a7a8d15ef0ac9a6 to your computer and use it in GitHub Desktop.
Save t6847kimo/c3ed7464a903eb1b3a7a8d15ef0ac9a6 to your computer and use it in GitHub Desktop.
// Program to calculate L1 / L2 cache size, compile in g++ -O1
// Platform : Intel Xeon(R) CPU E5-2667 0 @ 2.90GHz
// OS: Ubuntu 12.04
// Author : Yancy Chien
#include <iostream>
#include <string>
#include <sys/time.h>
#include <cstdlib>
using namespace std;
#define CACHE_LINE_SIZE (64) // set to correct to mutiply the difference
#define ARRAY_SIZE (128 * 1024 * 1024) // arbitary array size, must in 2^N to let module work
void access_array(char* arr, unsigned size)
{
const int loop_cnt = 64 * 1024 * 1024;
for (int i = 0; i < LOOP_CNT; i++)
{
arr[(i * CACHE_LINE_SIZE) & (size - 1)] += 10;
}
}
int main(int argc, char** argv){
double cpu_us_used;
struct timeval start, end;
for(int size = CACHE_LINE_SIZE * 2 ; size <= ARRAY_SIZE ; size *= 2){
char* arr = new char[size];
gettimeofday(&start, NULL); // get start clock
access_array(arr, size);
gettimeofday(&end, NULL); // get end clock
cpu_us_used = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
cout << size << " , " << cpu_us_used << endl;
delete[] arr;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment