Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 28th, 2007, 12:33 PM
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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining if form is closed Paula222 Access VBA 2 April 17th, 2006 02:09 AM
Determining the name of function dotnetprogrammer VS.NET 2002/2003 0 October 23rd, 2005 04:46 AM
Loading a jpg and determining the pixel size jaucourt Flash (all versions) 0 January 19th, 2005 07:09 AM
Determining DirectoryItem Type [email protected] VB.NET 0 June 3rd, 2004 05:30 PM
Determining size of recordset column gp_mk ADO.NET 0 May 27th, 2004 04:12 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.