#include<stdio.h>
#include<time.h>
main()
{
int t1;
int t2;
t1=clock();
t2=clock();
printf("%d",t2-t1);
}
为什么结果是0?而在VC下调试的时候显示t2,t1和t2-t1都是有值的,而且还不小,请教了
#include<time.h>
main()
{
int t1;
int t2;
t1=clock();
t2=clock();
printf("%d",t2-t1);
}
为什么结果是0?而在VC下调试的时候显示t2,t1和t2-t1都是有值的,而且还不小,请教了
clock的函数为clock_t clock( void );
所以你的t1,t2应该定义为clock_t,而不是int
有值那是因为你定义后产生的随机数
{
long i = 600000L;
clock_t start, finish;
double duration; /* 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( goal > clock() )
;
}