暂时还没发现可以在VC界面直接查看程序运行时间及内存占用的方法,以下方法都是从网上看来的,这里只做总结并且是通过代码实现的,如果有哪位高手知道更简便的方法,麻烦留言告诉我 , O(∩_∩)O
方法一:
#include <time.h>
long int begin,end;
time(&begin);
--------运行的程序段 ------
time(&end);printf("%d",end-begin);
方法二:
double start,finish;
start=(double)clock();
--------运行的程序段 ------
finish=(double)clock();
printf("%.4fms",finish-start);
方法三:
windows程序下的:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define N 10
int cmp(const void *a,const void *b)
{
int *pa = (int*)a,*pb = (int *)b;
return *pa-*pb;
}
int main()
{
int i;
clock_t start,finish;
double process;
int sum[N]={10,9,8,7,6,5,4,3,2,1};
start = clock();
printf("original sort.\n");
for(i=0;i<N;i++)printf("%d ",sum[i]);
printf("\n\n");
qsort(sum,N,sizeof(int),cmp);
printf("after qsort.\n");
for(i=0;i<N;i++)printf("%d ",sum[i]);
printf("\n\n");
finish = clock();
process = (double)(finish - start); //快速排序和时间统计.
printf("time you spend is %lf sec.\n\n",process/CLOCKS_PER_SEC);
return 0;
}
GCC运行环境:
//时间函数
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define N 10
int cmp(const void *a,const void *b)
{
int *pa = (int*)a,*pb = (int *)b;
return *pa-*pb;
}
int main()
{
int i;
time_t tim;
struct tm *at;
char now[80];
time(&tim);
at = localtime(&tim);
printf("local time is.\n");
strftime(now,79,"%Y-%m-%d\n%H:%M:%S\n",at);
puts(now); //打印当前时间
printf("\n");
clock_t start,finish;
start = clock();
double process;
int sum[N]={10,9,8,7,6,5,4,3,2,1};
printf("original sort.\n");
for(i=0;i<N;i++)printf("%d ",sum[i]);
printf("\n\n");
qsort(sum,N,sizeof(int),cmp);
printf("after qsort.\n");
for(i=0;i<N;i++)printf("%d ",sum[i]);
printf("\n\n");
finish = clock();
process = finish - start; //快速排序和时间统计.
printf("time you spend is %lf sec.\n\n",process/CLOCKS_PER_SEC);
time_t timep; //打印时间的第二第三种方式
time(&timep);
printf("local time is : %s\n",asctime(gmtime(&timep)));
printf("local time is : %s\n",ctime(&timep));
return 0;
}