如何使用clock_gettime

时间:2019-04-21 06:42:24

标签: c

我正在开发一个用于测量L1缓存的行大小的程序。查找它,我知道它是64个字节。我可以看到我所期望的答案,但是仅当在终端上打印时才可以。

#include <stdio.h>
#include <stdlib.h>  // for exit(), rand(), strtol()
#include <pthread.h>
#include <time.h>    // for nanosleep()
#include <errno.h>   // for EINTR error check in millisleep()
#include <stddef.h>
#include <stdio.h>
#include <sys/sysinfo.h>
#include <math.h>


float stdsum , standardDeviation = 0.0;;

int timeSum = 0;

long diff_time(struct timespec *t1, struct timespec *t2)
{
return t2->tv_nsec - t1->tv_nsec + (t2->tv_sec - t1->tv_sec) * 1000000000L;
}

void sumTimes(float data){
       stdsum += data;
}


void floodCache(int* x,FILE * file){
  int z;
  for(int z = 0; z<10000;z++){
    fprintf(file,"%d",(*x)+z);
  }
}

int main(int argc, char *argv[])
{

FILE * fp , *xx;
   /* open the file for writing*/
   fp = fopen ("./output","w");
     xx = fopen ("./DUMP","w");



struct timespec t1, t2;
int nullTime;
int i , j ,k , r;
int d;
int sum;

int arr1[10000] = {0};
int arr2[10000] = {0};

int arrTimes[10] = {0};

for(i = 0;i<10000;i++){
  r = rand()%60;
  arr1[i] = r;
  arr2[i] = i;
}
for(i=0;i<1000000;i++){
    clock_gettime(CLOCK_MONOTONIC, &t1);
    clock_gettime(CLOCK_MONOTONIC, &t2);
    d = diff_time(&t1, &t2);
    timeSum += d;
}
nullTime = timeSum/1000000;
timeSum = 0;
printf("nullTime = %d\n",nullTime);

// return(0);

int spacing = 0;
int reps = 1000000;

// for (i = 1; i < 100; i*2) {

    floodCache(arr1,xx);

  for (j = 0; j < reps; j++) {
    fprintf(fp,"arr[ %d ] ",(0 +(j*spacing)%10000));

    clock_gettime(CLOCK_MONOTONIC, &t1);
    sum =+ arr1[(0 +(j*spacing)%10000)];
    clock_gettime(CLOCK_MONOTONIC, &t2);

    fprintf(xx,"\n\n%d\n",sum);

    d = diff_time(&t1, &t2);
    timeSum += (d-nullTime);

    fprintf (fp, "%d\n",d);
// sleep(1);
}
 k=0;
  arrTimes[k] = (timeSum/reps);
  fprintf (fp, ">>>>>>>>>>>>>>>>spacing of %d, avg run time = %d \n",spacing,timeSum/reps);
  timeSum = 0;
  sum = 0;


floodCache(arr1,xx);

for (i = 1; i <= 128; i=i*2) {

  // for(k=0;k<5;k++){
 spacing =  i;
  for (j = 0; j < reps; j++) {
    printf("arr[ %d ] ",(0 +(j*i)%10000));
    r = rand();
    clock_gettime(CLOCK_MONOTONIC, &t1);
    sum =+ arr2[(0 +(j*spacing)%10000)];
    // arr2[(0 +(j*spacing)%10000)] 
    clock_gettime(CLOCK_MONOTONIC, &t2);

    fprintf(xx,"\n\n%d\n",sum);

    d = diff_time(&t1, &t2);
    timeSum += (d-nullTime);

    printf ( "%d\n",d);
// sleep(1);
}
  fprintf (fp, ">>>>>>>>>>>>>>>>spacing of %d, avg run time = %d \n",spacing,timeSum/reps);
  arrTimes[k] = (timeSum/reps);
  k++;
  timeSum = 0;
  sum = 0;

floodCache(arr1,xx);
  // }
}
for(i=0;i<10;i++){
  printf("spacing of %d = %d \n",i,arrTimes[i]);
}
fclose(xx);
fclose(fp);



}

因此,在最终打印输出时,指示的间距增加到该倍数2。正如粘贴的那样,间距4(2 ^ 4 = 16,16 * sizeof(int)= 64 )的执行长度会发生明显的跳跃,这正是我期望看到的。但是,如果我将这些数据打印到文件中,或者不将其打印到任何地方,则所需的时间要少得多,并且不会显示出指示缓存深度发生变化的模式。

所以有2个问题。 1,我是否以正确的方式解决问题,以及2为什么随我打印的位置而有所不同。

0 个答案:

没有答案