如何计算程序中一个作业的时间?

C、C++和Java语言
回复
flyinflash
帖子: 2376
注册时间: 2006-09-21 14:28
送出感谢: 0
接收感谢: 0

如何计算程序中一个作业的时间?

#1

帖子 flyinflash » 2008-07-06 15:24

如何计算程序中一个作业的时间?

我最近在做一个小程序
以 ANSI C 作为语言核心,实现常见各种内部排序算法,提供简单操作接口,让用户可以直观了解各
算法原理、性能、时空复杂度。
怎么算独立算各个排序作业的时间?

我想到的是

代码: 全选

time_t s, e;
s = time(0);
sort();
e = time(0);
printf("\n %f ", difftime(e, s));
但是,很显示,我的做法是错误的。
flyinflash
帖子: 2376
注册时间: 2006-09-21 14:28
送出感谢: 0
接收感谢: 0

#2

帖子 flyinflash » 2008-07-06 15:26

代码: 全选

lee@lee-thinkpad:$ time ./bubble_selection
real	0m1.835s
user	0m0.008s
sys	0m0.012s
real 时间似乎就是我想要的
flyinflash
帖子: 2376
注册时间: 2006-09-21 14:28
送出感谢: 0
接收感谢: 0

#3

帖子 flyinflash » 2008-07-07 19:24

// time.c

代码: 全选

#include <sys/time.h>
#include <stdio.h>
#include <math.h>
#include "funcs.h"

/*
struct timeval
{
  long tv_sec;
  long tv_usec;
};
*/

//gettimeofday把时间保存在结构tv之中.tz一般我们使用NULL来代替. 

int gettimeofday(struct timeval *tv, struct timezone *tz); 

void function() 
{
	unsigned int i, j; 
	double y;
	
	for(i = 0; i < 1000; i++) 
		for(j = 0; j < 1000; j++) 
			y = sin( (double)i ); 
}


int main()
{ 
	struct timeval tpstart, tpend; 
	float timeuse; 
	
	gettimeofday(&tpstart, NULL); 
	
	//function();
	si();
	
	gettimeofday(&tpend, NULL);
	
	timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec - tpstart.tv_usec;
	
	timeuse /= 1000000;
	
	printf(" Used Time:%f \n", timeuse); 
	
	
	gettimeofday(&tpstart, NULL); 
	
	//function();
	bs();
	
	gettimeofday(&tpend, NULL);
	
	timeuse = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec - tpstart.tv_usec;
	
	timeuse /= 1000000;
	
	printf(" Used Time:%f \n", timeuse); 
	
	return 0;
}
flyinflash
帖子: 2376
注册时间: 2006-09-21 14:28
送出感谢: 0
接收感谢: 0

#4

帖子 flyinflash » 2008-07-07 19:25

// funcs.h

代码: 全选

#ifndef __FUNCS_H__
#define __FUNCS_H__


#include <lee/stdiol.h>
#include <lee/al_rand.h>
#include "funcs.c"


int times = 0;

void echo(int *d, int times);
int create_datas();
int *read_datas();

  
#endif
// funcs.c

代码: 全选

#include <stdio.h>
#define FILE_NAME "datas.txt"

void echo(int *d, int times)
{
  int i;
  
  printf("\n");
  printf("\n	TIMES %d ::  ", ++times);
  for (i = 1; d[i]; i++)
    printf(" %d ", d[i]);
}


int create_datas()
{
  int i;
  int max;
  int *d;
  FILE *fp;
  
  
  printf("\n	MAX_RECORDS = ");
  scanf("%d", &max);
  getchar();
  
  d = (int *)malloc(sizeof(int) * max);
  
  for (i = 0; i < max; i++)
    d[i] = rand_num(10, 99);
    
    
  if ( NULL != (fp = fopen(FILE_NAME, "w")) )
  {
    fprintf(fp, "%d\n", max);
    
    for (i = 0; i < max - 1; i++)
    {
      fprintf(fp, "%d\n", d[i]);
    }
    fprintf(fp, "%d", rand_num(10, 99));
    
    
    fclose(fp);
    
    printf("\n");
    printf("\n	write SUCCESSFULLY ");
    
    return max;
  }
  else
  {
    system("clear");
    
    printf("\n");
    perror("\n	write FAILED ");
    
    return -1;
  }
}


int *read_datas()
{
  int i;
  int max;
  int *d;
  FILE *fp;
    
    
  if ( NULL != (fp = fopen(FILE_NAME, "r")) )
  {
    fscanf(fp, "%d\n", &max);

    d = (int *)malloc(sizeof(int) * max);
    d[0] = max;
    
    for (i = 1; i < max; i++)
    {
      fscanf(fp, "%d\n", &(d[i]));
    }
    
    fclose(fp);
    
    printf("\n");
    printf("\n	read SUCCESSFULLY ");
    
    return d;
  }
  else
  {
    system("clear");
    
    printf("\n");
    perror("\n	read FAILED ");
    
    return NULL;
  }
}



int si()
{
  int *d;
  int i, j, max;
  
  
  printf("\n");
  printf("\n		Insertion Sort | Straight Insertion Sort ");
  
  if ( NULL == (d = read_datas()) )
    create_datas();
    
  max = d[0];
  
  for (i = 2; i < max; i++)
  {
    d[0] = d[i];
    j = i - 1;

    while (d[0] < d[j])
    {
      d[j + 1] = d[j];
      j--;
    }
    d[j + 1] = d[0];
    
    //	echo(d, times++);
  }

	return 0;
}



int bs()
{
  int *d;
  int i, j, mv;
  int tmp;

  
  printf("\n");
  printf("\n		Selection Sort | Bubble Selection Sort ");
  
  if ( NULL == (d = read_datas()) )
  	create_datas();
  	
    
	for (i = 2; i < d[0]; i++)
  {
  	mv = 0;
  	
  	for (j = 2; j < d[0]; j++)
  		if (d[j] < d[j - 1])
  		{
				tmp = d[j - 1];
				d[j - 1] = d[j];
				d[j] = tmp;
				mv = 1;
			}
			
		if (!mv)
			break;
			
    //	echo(d, times++);
  }

	return 0;
}
缩写说明

Straight Insertion Sort
si()

Bubble Selection Sort
bs()

注意,没有使用到多线程。
回复

回到 “C/C++/Java”