Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
printf如何影响getrusage在C中测量时间_C_Printf_Getrusage - Fatal编程技术网

printf如何影响getrusage在C中测量时间

printf如何影响getrusage在C中测量时间,c,printf,getrusage,C,Printf,Getrusage,我可能错过了getrusage()函数的一些要点 当我运行以下代码时,我得到: 用户时间:0.000000s 系统时间:0.000000s 总时间:0.000000s void naiveSearch(unsigned char *text, unsigned int textLen, unsigned char *pattern, unsigned int pattLen) { register unsigned int count = 0; for(int i=0;i<=te

我可能错过了getrusage()函数的一些要点

当我运行以下代码时,我得到:

用户时间:0.000000s 系统时间:0.000000s 总时间:0.000000s

void naiveSearch(unsigned char *text, unsigned int textLen, unsigned char *pattern, unsigned int pattLen) 
{
  register unsigned int count = 0;
  for(int i=0;i<=textLen-pattLen;i++)
  {
    int j;
    for(j=0;j<pattLen;j++)
      if((unsigned char)text[i+j] != (unsigned char)pattern[j])
        break;

    if(j == pattLen)
       count++;
  }
  //printf("naiveSearch: count = %d\n",count);
}

int main(int argc, char * argv[])
{
  struct rusage ruse;
  double ssec1, ssec2, usec1, usec2;

  getrusage(RUSAGE_SELF,&ruse);
  ssec1 = (double)(ruse.ru_stime.tv_sec * 1000000 + ruse.ru_stime.tv_usec);
  usec1 = (double)(ruse.ru_utime.tv_sec * 1000000 + ruse.ru_utime.tv_usec);

  for(int k=0;k<1000;k++)
    naiveSearch(writeBuffer,outSize,writeBuffer+rand,16);

  getrusage(RUSAGE_SELF,&ruse);
  ssec2 = (double)(ruse.ru_stime.tv_sec * 1000000 + ruse.ru_stime.tv_usec);
  usec2 = (double)(ruse.ru_utime.tv_sec * 1000000 + ruse.ru_utime.tv_usec);

  printf("User time:\t%f s\n",(usec2 - usec1)/(double)1000000);
  printf("System time:\t%f s\n",(ssec2 - ssec1)/(double)1000000);
  printf("Total time:\t%f s\n",((usec2 + ssec2) - (usec1 + ssec1))/(double)1000000);

  return 0;
}
void naiveSearch(无符号字符*文本、无符号整数文本、无符号字符*模式、无符号整数模式)
{
寄存器无符号整数计数=0;

对于(int i=0;i我认为您的程序没有使用足够的CPU来让计时器显示任何内容。时钟滴答声可能不够敏感,您的naiveSearch函数无法生成任何内容。此外,也没有理由将时间转换为双倍。请尝试这样做:

#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>

int runalot()
{
    int i = 0;
    int j = 1;

    for (i = 0; i < 100000000; i++) {
        if (i % j == 4) {
            j += 1;
        }
    }
    return j;
}

int main(int argc, char * argv[])
{
    int j;
    struct rusage ruse1, ruse2;
    double ssec1, ssec2, usec1, usec2;

    getrusage(RUSAGE_SELF,&ruse1);
    j = runalot();
    printf("runalot returns %d\n", j);
    getrusage(RUSAGE_SELF,&ruse2);
    printf("CPU usage is: %lu\n", ruse2.ru_utime.tv_sec * 1000000 + ruse2.ru_utime.tv_usec - ruse1.ru_utime.tv_sec * 1000000 + ruse1.ru_utime.tv_usec);
}
#包括
#包括
#包括
int runalot()
{
int i=0;
int j=1;
对于(i=0;i<100000000;i++){
如果(i%j==4){
j+=1;
}
}
返回j;
}
int main(int argc,char*argv[])
{
int j;
结构rusage ruse1、ruse2;
双ssec1,ssec2,usec1,usec2;
getrusage(RUSAGE_SELF和ruse1);
j=runalot();
printf(“runalot返回%d\n”,j);
getrusage(RUSAGE_SELF和ruse2);
printf(“CPU使用率为:%lu\n”,ruse2.ru_utime.tv_sec*1000000+ruse2.ru_utime.tv_usec-ruse1.ru_utime.tv_sec*1000000+ruse1.ru_utime.tv_usec);
}

当我在Linux上用gcc在-g调试标志下编译它并运行它时,我得到的
CPU使用率是:372000

几乎可以肯定编译器已经优化了你的代码,因为它什么也不做,试着返回计数并打印它,现在编译器不能扔掉代码。什么编译器、什么标志和plz给出了实际的可复制代码。M我猜你的程序很可能没有使用太多的CPU。为什么不试着打印一些其他统计数据。我使用以下gcc标志:CFLAGS=-O3-mavx-msse2。我猜这一定是由于一些编译器优化造成的。这不能是因为CPU使用率低,因为我在100 MB文本文件上测试了搜索算法。这迫使我更新/扩展d我的问题(见我原来帖子的结尾)。-O3告诉编译器优化内容,用-O0完成