您的位置:首页 > 编程语言

计算指令执行时间代码

2011-12-29 09:06 309 查看
秒级别的:
#include<stdio.h>

#include<time.h>

#include<stdlib.h>

int main()

{

time_t cur;

time(&cur);

printf("opencur1=%d\n",cur);

FILE *file=fopen("a.txt","r");

if(file==NULL){printf("file open failed!\n");}

time(&cur);

printf("opencur2=%d\n",cur);

typedef struct{

int id;

char name[10];

double sal;

}Emp;

Emp*es2=malloc(sizeof(Emp));

time(&cur);

printf("readcur1=%d\n",cur);

int n=fread(es2,sizeof(Emp),1,file);

time(&cur);

printf("readcur2=%d\n",cur);

fclose(file);

for(int i=0;i<n;i++){

printf("%d,%s,%g\n",es2[i].id,es2[i].name,es2[i].sal);

}

free(es2);

}

毫秒级别的:

#include<stdio.h>

#include<sys/time.h>

#include<stdlib.h>

int main()

{

struct timeval t_start,t_end;

long const_timeopen=0;

long const_timeread=0;

//get opent sart time

gettimeofday(&t_start,NULL);

printf("start open time :%ld us\n",t_start.tv_usec);

FILE *file=fopen("test.info.swp","r");

if(file==NULL){printf("file open failed!\n");}

//get end time

gettimeofday(&t_end,NULL);

printf("end open time:%ld us\n",t_end.tv_usec);

//const open time

const_timeopen=t_end.tv_usec-t_start.tv_usec;

printf("const open time :%ld us\n",const_timeopen);

typedef struct{

int id;

char name[10];

double sal;

}Emp;

Emp*es2=malloc(sizeof(Emp));

//get read sart time

gettimeofday(&t_start,NULL);

printf("start read time :%ld us\n",t_start.tv_usec);

int n=fread(es2,sizeof(Emp),1,file);

//get end time

gettimeofday(&t_end,NULL);

printf("end read time:%ld us\n",t_end.tv_usec);

//const read time

const_timeread=t_end.tv_usec-t_start.tv_usec;

printf("const read time :%ld us\n",const_timeread);

fclose(file);

for(int i=0;i<n;i++){

printf("%d,%s,%g\n",es2[i].id,es2[i].name,es2[i].sal);

}

free(es2);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: