计算指令执行时间代码
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);
}
#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);
}
相关文章推荐
- VC++如何计算一段代码的执行时间
- 解析PHP计算页面执行时间的实现代码
- System.currentTimeMillis()获取时间,计算代码执行效率
- 代码执行时间计算
- php 计算代码执行时间
- iOS计算一段代码执行时间
- php 页面执行时间计算代码
- php计算代码执行时间
- 精确计算代码执行时间
- c/c++ 计算代码执行时间
- 代码片段 -- Java计算代码执行消耗时间
- C#计算代码执行时间的方法
- Python:计算一段代码的执行时间
- php 计算代码执行时间
- 代码片段 -- Java计算代码执行消耗时间
- python3 timeit:计算小段代码的执行时间的模块
- c++计算代码执行时间的方法,毫秒级
- C#如何计算代码执行时间
- 精确计算代码执行时间
- QT 计算 某一段代码执行时间