View Single Post
  #1 (permalink)  
Old January 28th, 2007, 12:33 PM
easwars easwars is offline
Registered User
 
Join Date: Jan 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Determining the cache size

Hi All,
I am trying to write code which will determine the size of the cache memory on the system. The logic I use is that I have a big array and I read in parts of the array and manipulate on it. I start from array sizes of 2KB and go upto sizes of 16MB. I measure the time required to manipulate these subarrays. I expected a sharp increase in this time once the array size gets bigger than the cache size. But when i execute my code, i dont see such a sharp increase. Can anybody explain why this is happening.

Thanks a lot

cache.cpp
========
#include <iostream>
#include <cmath>

#include "PCTime.h"

using namespace std;

double mem[4096000];

int main()
{
    PCTime start, end;

    long num = sizeof(mem)/sizeof(double);
    double min = 0;

    for(int k=256; k<=num ; k=k*2)
    {
        min = 10;
        for(int j=0 ; j<10 ; j++)
        {
            start.read();
            for(int i=0 ; i<k; i+=1)
            {
                mem[i] = (mem[i] * sqrt((double)i));
            }
            end.read();
            if(j!=0)
            {
                if(min > (end - start).seconds())
                    min = (end - start).seconds();
            }
        }
        double avg_time = (min)/k;
        cout << "No. of seconds is \t" << avg_time << "\t for size \t" << (k*8)/1024 << "KB" << endl;
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

PCTime.h
=======
// PCTime.h -- A reasonably-accurate timer based on the x86 Performance Counters

#ifndef PCTIME_H_
#define PCTIME_H_

#include <windows.h> // needed for QueryPerformanceCounter/QueryPerformanceFrequency

#include <iostream>

using namespace std;

class PCTime {
public:
       PCTime(long long x=0) { this->x = x; }
       long long x;
       operator long long(void) const {
               return x;
       }
       double seconds(void) const {
               long long freq = 0;
               QueryPerformanceFrequency((PLARGE_INTEGER) &freq);
               return (double) x / freq;
       }
       PCTime operator-(const PCTime& that) {
               return PCTime(x - that.x);
       }
       void read(void) {
               QueryPerformanceCounter((PLARGE_INTEGER) this);
       }
};


inline
std::ostream& operator<<(std::ostream& out, PCTime pcnt) {
       double res = pcnt.seconds();
       if (res > 1) {
               out << res << "s";
       } else if (res > 0.001) {
               out << 1000 * res << "ms";
       } else if (res > 1e-6) {
               out << 1e6*res << "us";
       } else {
               out << 1e9*res << "ns";
       }
       return out;
}


#endif /*PCTIME_H_*/

Reply With Quote