我正在尝试对缓存进行跨步测试,但是我的代码中存在错误,导致该程序永无休止。我必须自己终止文件。这应该是代码中的一个简单错误,但我真的找不到它在哪里。任何帮助将不胜感激。这是代码:
#include <iostream>
using namespace std;
#include <fstream>
#include<time.h>
#include<stdlib.h>
typedef unsigned char byte;
int main(int argc, char **argv)
{
if(argc!=3)
{
cout<<"Usage is: "<<argv[0]<< " [stride] [steps]"<<endl;
return 0;
}
int stride = atoi(argv[1]);
int steps = atoi(argv[2]);
int totalSize = stride * steps;
// print some info about the system
cout<<"size of int: "<<sizeof(int)<<endl;
cout<<"stride: "<<stride<<endl;
cout<<"total size: "<<totalSize<<endl;
byte *dataArray = new byte[totalSize];
// init array data
//for(int i=0;i<totalSize;i++)
// dataArray[i] = 1;
// traverse array and measure time
struct timespec start, stop;
//save start time
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start);
long result = 0;
for(int i=0; i<totalSize;i + stride)
{
result += dataArray[i];
}
for(int i=0;i<totalSize;i+=stride)
result += dataArray[i];
//save end time
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stop);
ofstream myfile;
// myfile.open("data.csv");
// calc elapsed time
double cpuTime = (stop.tv_sec - start.tv_sec) * 1e6 + (stop.tv_nsec - start.tv_nsec) / 1e3; // in micros
// myfile << "%d",cpuTime;
// myfile.close();
// report results
cout<<"Calculation: "<<result<<endl;
cout<<"CPU time micro: "<<cpuTime<<endl;
return 0;
}
for(int i=0; i<totalSize;i + stride)
您永远不会增加i
。