time함수와 gettimeofday 함수의 차이점..

psjcap의 이미지

안녕하세요..

gettimeofday 함수와 time 함수를 섞어서 사용했는데..
사용하다보니.. 가끔 두 함수의 리턴결과가 차이가 나는 경우가 발생합니다..
물론 한 프로그램에서 순서데로 호출이다 보니..
약간의 차이는 당연히 발생할 수 있습니다만..
무한루프를 돌게하면서.. 계속 결과를 출력하여 보았더니..
gettimeofday 함수가 time 함수보다 평균적으로 0.07초 정도가 빠르더군요..

이 두 함수가 시간을 얻어오는 차이점에 대해서 혹시 알고 계신 분이 계신가해서 질문 올립니다..
그럼 좋은 하루 되셔요..^^"

sangwoo의 이미지

보통 time(3)이 gettimeofday(2)를 이용해서 구현되어 있습니다.
함수 호출 오버헤드때문에 time() 이 조금 더 긴 시간이 걸리는 것 아닐까요?
FreeBSD libc의 구현을 첨부합니다 :-)

time_t
time(t)
        time_t *t;
{
        struct timeval tt;
        time_t retval;

        if (gettimeofday(&tt, (struct timezone *)0) < 0)
                retval = -1;
        else
                retval = tt.tv_sec;
        if (t != NULL)
                *t = retval;
        return (retval);
}

----
Let's shut up and code.

psjcap의 이미지

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

int main()
{
    time_t  stTime;
    timeval stTm;

    for( ;; )
    {
        gettimeofday( (timeval*)&stTm, NULL );
        stTime = time( NULL );

        if( stTime != stTm.tv_sec /* && stTm.tv_usec > 10000  */ )
            printf( "[Time Error] time(%d)  gettimeofday(%d, %d)\n", stTime, stTm.tv_sec, stTm.tv_usec );

    }
}

위 소스를 실행하면..

[Time Error] time(1086162243) gettimeofday(1086162244, 5677)
[Time Error] time(1086162243) gettimeofday(1086162244, 5715)
[Time Error] time(1086162243) gettimeofday(1086162244, 5753)
[Time Error] time(1086162243) gettimeofday(1086162244, 5791)
[Time Error] time(1086162243) gettimeofday(1086162244, 5830)
[Time Error] time(1086162243) gettimeofday(1086162244, 5869)
[Time Error] time(1086162243) gettimeofday(1086162244, 5907)
[Time Error] time(1086162243) gettimeofday(1086162244, 5945)

즉 gettimeofday 함수가 먼저 호출되었지만..
먼저 호출되었으므로 느린게 맞겠지만..
실제론 time보다 시간이 더 빠릅니다..

테스트 환경은 Solaris 8입니다..

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.