2008年6月7日 星期六

測量 CLOCK and Times

自己寫的在 linux 下面測量時間的方式 , 跟大家分享一下 。

typedef struct clock_info {

char name[20];
clock_t start;
clock_t end;
clock_t total;
clock_t times;
clock_t max;
clock_t min;
clock_t tmp_dur;
} clock_info;

void clock_info_Init( clock_info *ci , char *name)
{
strncpy(ci->name, name, 20);
ci->start = 0 ; ci->end = 0; ci->total = 0;
ci->times = 0 ; ci->max = 0; ci->min = 0;
ci->tmp_dur = 0;
}
void clock_info_sample_start ( clock_info *ci, clock_t sample )
{
ci->start = sample;
}
void clock_info_sample_end ( clock_info *ci , clock_t sample )
{
ci->end = sample;
ci->tmp_dur = ci->end - ci->start;
ci->total += ci->tmp_dur;
ci->times ++;
if ( ci->times == 1 )
{
ci->max = ci->tmp_dur;
ci->min = ci->tmp_dur;
}
if ( ci->tmp_dur > ci->max )
ci->max = ci->tmp_dur;

if ( ci->tmp_dur <>min )
ci->min = ci->tmp_dur;
}
void clock_info_print ( clock_info *ci )
{
char str[255];
sprintf(str," CLOCK Measurement report CLOCKS_PER_SEC = %d\n", CLOCKS_PER_SEC);
printf(str);

double avg = (double)( (double) ci->total / (double) ci->times ) ;
double max = (double) ci->max;
double min = (double) ci->min;
sprintf(str," <%s>\t max : %7.5f (clk) \t min : %7.5f (clk) \t avg : %7.5f (clk) \n",
ci->name, max , min , avg);
printf(str);

avg = ( avg / (double)CLOCKS_PER_SEC );
max = ( max / (double)CLOCKS_PER_SEC );
min = ( min / (double)CLOCKS_PER_SEC );
sprintf(str," <%s>\t max : %7.5f (s) \t min : %7.5f (s) \t avg : %7.5f (s)\n",
ci->name, max , min , avg );
printf(str);

avg = avg * 1000.0;
max = max * 1000.0;
min = min * 1000.0;
sprintf(str," <%s>\t max : %7.5f (ms) \t min : %7.5f (ms) \t avg : %7.5f (ms) \n",
ci->name, max , min , avg);
prinf(str);
}

沒有留言: