<pre id=\"best-answer-content\" class=\"reply-text mb10\">可以用clock()函数, 返回自程序开始运行所经过的时间,返回值为clock_t , 转换返回值为秒, 返回值除以CLOCKS_PER_SEC.
#include stdio.h
#include time.h
int c = 0;
int main(int argc, char *argv[])
{
clock_t a =clock();
for(int i=0; i9000 ; i )
{
for(int j=0; j9000 ; j )
{
c;
}
c -= 9000;
}
clock_t b =clock();
long doubled = b - a;
long doubletime = d/CLOCKS_PER_SEC;
printf(\"time:%g\", time);
return 0;
}
这个是微软的程序
#include stdio.h
#include stdlib.h
#include time.h
void sleep( clock_t wait );
void main( void )
{
long
i = 600000L;
clock_t start, finish;
doubleduration;
/* Delay for a specified time. */
printf( \"Delay for three seconds\\n\" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( \"Done!\\n\" );
/* Measure the duration of an event. */
printf( \"Time to do %ld empty loops is \", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( \"%2.1f seconds\\n\", duration );
}
/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait clock();
while( goalclock() )
;
} |