C에서 1초 이하를 다루는 가장 좋은 방법은 무엇일까요?

0
points

Linux 에서 C로 프로그래밍을 할 때

1초 이하의 시간을 다루는 가장 좋은 방법은 무엇일까요???

(경과한 시간을 잰다거나....특정시간동안 중단, 혹은 loop를 돈다거나.)

getitimer
setitimer

함수 처럼 복잡한 방법이 아니라.

인용:
time_t t1, t2;
t1 = time ( 0 );
...................
...................
do_any_thing
...................
...................
t2 = time ( 0 );

처럼 간단히 해서.... t2 - t1 하면 1.375 Sec 하고 결과가 나올 수 있는
방법은 없을까요?????

[b]gettimeofday()[/b]에서 timeval 을 이용

1
point

gettimeofday()

에서 timeval 을 이용하시면 됩니다.

struct timeval {
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};

second 로 하는 것은 difftime(3) 이라는 것이 있지만
함수의 수행 시간을 계산하는 것은 아마 그것으로 불가능할 겁니다..

#include <stdio.h>
#include <sys/time.h>

#define MULTIPLE 10000  /* 이 값은 정확히 몇 배수인지 기억 않 남.. */

long calculate_execution_time(struct timeval begin, struct timeval end);

int
main(void)
{
    struct timeval start_time, finish_time;

    gettimeofday(&start_time, NULL);

    some_func();

    gettimeofday(&finish_time, NULL);

    printf("%ld microsec.. \n", calculate_execution_time(start_time, finish_time));
}

long
calculate_execution_time(struct timeval begin, struct timeval end)
{
    return  (end.tv_sec/MULTIPLE + end.tv_usec) - (begin.tv_sec/MULTIPLE + begin.tv_usec);
}

이게 좀 더 근접한 코드일듯

1
point

int main ( void )
{
struct timeval start, stop, running_time;

gettimeofday ( &start, NULL );

sleep(2);

gettimeofday ( &stop, NULL );

if ( stop.tv_usec <= start.tv_usec )
{
stop.tv_sec --;
stop.tv_usec += 1000000;
}
running_time.tv_sec = stop.tv_sec - start.tv_sec;
running_time.tv_usec = stop.tv_usec - start.tv_usec;

printf ("Running Time : %d.%06d\n", running_time.tv_sec, running_time.tv_usec );
}

timespec 구조체가 있습니다.

0
points

timespec 구조체를 사용하는 함수는 nano second 단위로 시간을 연산할 수 있습니다.

시간을 얻어오기 위해서 clock_gettime() 이런 함수를 사용하면 될테고

sleep 하기 위해서는 nanosleep() 을 사용하면 되겠군요..

참고로 usleep() 이 있는데 이건 MTLevel이 Unsafe 하므로 비추(?) 입니다.

cdpark의 이미지
6852
points

gettimeofday 대신에 getrusage로 얻은 값을 사용하세요.

0
points

gettimeofday 대신에 getrusage로 얻은 값을 사용하세요.

이건 실제 CPU 사용 시간을 재는 거라 다른 프로그램이 돌고 있는 경우에도 그 차이가 적습니다. (물론 아예 오차가 없는 건 아니지만요.)

winchild의 이미지
1250
points

SELECT() 함수를 사용해 보시지요.

0
points

시간 맘대루 조정 가능하구요.
마이크로 세컨드 까지 가능합니다.

거기다 시스템 부하를 거의 주지 않구요.
기다리는 시간에 다른일도 할수 있습니다.

쪼매 사용하기는 좀 어렵지요.

- 겨울아지 -

select 함수를 이용해도 wait 비슷한 효과를 얻을 수 있습니다.

0
points

select 함수를 이용해도 wait 비슷한 효과를 얻을 수 있습니다.
다음 코드를 참고하시길 바랍니다.

#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

int main(void)
{
    struct timeval tv;
    int count = 0;

    while (count++ < 100)
    {
        tv.tv_sec = 1;   //second
        tv.tv_usec = 0;  // Microsecond
        select(0, NULL, NULL, NULL, &tv);

        fprintf(stderr, "1 sec Test..\n");
    }

    return 0;
}

원래 첫번째부터 네번째 인자까지 올바른 값을 입력해야 하지만, 그냥 위와 같이 입력하시면
tv 구조체에서 설정한 시간대로 wait 하게 됩니다.

주의할 점은 한번 select 가 호출하고 나면 tv의 값이 초기화 되므로 위와 같이 select 를 호출하기 전에
다시 한번 시간을 설정해야 합니다. (리눅스 버전)
솔라리스 버전은 select 호출 이후에 tv의 값이 초기화 되지 않으므로 한번만 설정해도 됩니다.

여러분들의 답변에 감사드립니다.....

0
points

:D
많은 도움이 되었습니다. ^^

댓글 보기 옵션

원하시는 댓글 전시 방법을 선택한 다음 "설정 저장"을 누르셔서 적용하십시오.