#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都是有值的,而且还不小,请教了

解决方案 »

  1.   

    可以看一下MSDN嘛
    clock的函数为clock_t clock( void );
    所以你的t1,t2应该定义为clock_t,而不是int
    有值那是因为你定义后产生的随机数
      

  2.   

    两次 clock 调用时间太接近,值是一样的
      

  3.   

    clock 单位是毫米,分辨率太低,区分不出两次调用的间隔
      

  4.   

    记得clock_t是long型的赋值给int型应该是没问题吧
      

  5.   

    void sleep( clock_t wait );void main( void )
    {
       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() )
          ;
    }