藍森林首頁 | 返回主頁 | 本站地圖 | 站內搜索 | 聯繫信箱 |
 您目前的位置:首頁 > 自由軟件 > 技術交流 > 應用編程


    

藍森林 http://www.lslnet.com 2006年6月6日 10:18


c語言中的計時函數有沒有以毫秒為精度的?

由於課設需要,必須精確到毫秒。
哪個ggjjddmm知道的請告知具體的函數及其使用,謝謝! :wink:

c語言中的計時函數有沒有以毫秒為精度的?

你可以將它換算得到毫秒呀

c語言中的計時函數有沒有以毫秒為精度的?

man usleep

c語言中的計時函數有沒有以毫秒為精度的?

如果是睡眠的話
可以使用usleep或是select實現

但是取時間的話
這裡可以參考一下
http://docs.biostat.wustl.edu/cgi-bin/info2html?(libc.info.gz)Calendar%2520Time

c語言中的計時函數有沒有以毫秒為精度的?

[code]
#include <sys/types.h>;
#include <sys/times.h>;
#include <sys/select.h>;

void delay( int inter )
{
fd_set rfd_set;
struct timeval timeout;
int i;

timeout.tv_sec = inter / 1000;//秒
timeout.tv_usec = ( inter % 1000 ) * 1000;//微秒
i = select( 0,0,0,0,&timeout );
}

void main()
{
int ms=1000,i;

printf("print is begin!\n");

for(i=0;i<100;i++){
printf("%5ds.\n",i);
delay(ms);
}


[/code]

c語言中的計時函數有沒有以毫秒為精度的?

精確毫秒級別,用select

c語言中的計時函數有沒有以毫秒為精度的?

int Sleep(int msec)
{
    struct timeval timeout;

    memset(&timeout, 0, sizeof(struct timeval));
    if(msec >;= 1000)
        timeout.tv_sec=msec/1000;
    else
        timeout.tv_sec=0;

    timeout.tv_usec = (msec%1000)*1000;

    select(0, NULL, NULL, NULL, &timeout);

    return( 0 );
}

c語言中的計時函數有沒有以毫秒為精度的?

謝謝各位的解答,不過我還不太明白select的使用。
本人在課題中想計算一下一個程序執行的時間,必須精確到毫秒。
select函數是用於這方面計時的嗎? 如果是,請具體描述一下如何使用。
謝謝!

c語言中的計時函數有沒有以毫秒為精度的?

Linux平台?
man 2 times

不妨同時參考一下time(1)的源代碼。

c語言中的計時函數有沒有以毫秒為精度的?

-->     


你樓上的把示例代碼都給了你,你還要什麼?select的使用說明碼?如果是到置頂的FAQ找找吧。

c語言中的計時函數有沒有以毫秒為精度的?

用time command可以吧

c語言中的計時函數有沒有以毫秒為精度的?

/**************************************
  ** filename: gettimeofday.c
  ** 這個程序可以吧, 計算sleep(1)精確到微妙
***************************************
*/

#include <stdio.h>;
#include <stdlib.h>;
#include <unistd.h>;
#include <sys/time.h>;


int
main()
{
        struct timeval first;
        struct timeval second;
        struct timezone temp;
       
        gettimeofday( &first, &temp );
       
        sleep(1);

        gettimeofday( &second, &temp );
       
        printf("first  = %d , %d \n", first.tv_sec,  first.tv_usec );
        printf("second = %d , %d \n", second.tv_sec, second.tv_usec );
       
        return 0;               
}

c語言中的計時函數有沒有以毫秒為精度的?

man clock



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 藍森林網站 版權所有。 E-mail : webmaster@lslnet.com