gettimeofday()에 관련되서 질문 올립니다..

fiyfiy의 이미지

시작 시간과 끝 시간을 얻어온 다음 빼서

작업에 걸린시간어 몇 (us) 인지 얻어오려고 하는데..

정확히 맞지 않은거 같아서 이렇게 질문합니다..

제가 사용한 방법이 일단 맞은지.. 그리구 마이크로 단위의 시간을 얻어와야 하는데 gettimeofday말고

다른 함수 사용할수 있으면 방법좀.. 알려주세요..

그리구 제가 구현한게.. 자꾸 시간이 - 시간도 나오고 그러는데.. 제가 대충 재봤을때도 아에 맞지 않은거

같아서요.. 안되는 이유가 딱 눈에 보이시면 설명좀 부탁드려요 ㅡㅜ

#include

long nStarttime=0,nEndtime=0,nResult=0;

struct timeval start,end;

..

..

gettimeofday(&start, NULL);
nStarttime = start.tv_usec;

while(작업)

{

..

.. 정해진만큼 while 문수행하면서 작업..

}

gettimeofday(&end, NULL);
nEndtime = end.tv_usec;

printf("TotalTime = %lu \n",nEndtime -nStarttime);

이렇게 했는데 이상하게 되드라구요..

그리구 gettimeofday 에 조사하다보니깐 time.h 에 있는 timersub(1,2,result)

이것 사용하니깐 아에 리눅스에서 빌드 에러가 뜨드라구요..

리눅스에서 시간 구하고싶은데 전문가님들의 의견좀 부탁드려요..

ymir의 이미지

tv_usec 은 microsecond 로 0 ~ 999999 의 값을 갖습니다.
얘가 1000000(1sec) 가 되는 순간, tv_sec 이 ++ 되고 얘는 0 으로 리셋됩니다.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

duyoungk의 이미지

printf("TotalTime = %lu \n", (end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec);

이렇게 해보세요

superwisdom의 이미지

timersub 매크로를 사용하면 간단하게 시간 차이를 구할 수 있습니다. (sys/time.h)

dorado2의 이미지


tv_usec뿐 아니라 tv_sec까지 고려해서 시간 차이를 구하셔야 합니다.
지금 코드엔 그러한 부분이 없어서 시간이 잘못 나올 수 있지요.

http://kldp.org/node/34519 에 timersub 관련 매크로 내용이 있네요.

_BSD_SOURCE 정의를 해야 timersub 매크로 사용이 가능한 거 같은데,
따로 매크로 정의해서 쓰셔도 무방할 듯 하네요.

dragonwine의 이미지

1. 프로그램에서 직접 구하는법
struct timeval start, end, result;

gettimeofday(&start,NULL);

/* 작업 */

gettimeofday(&end, NULL);

timersub(&end, &start, &result);

printf("%u.%u 초\n", result.tv_sec, result.tv_usec/1000000);

2. 유틸리티
./time [실행파일]

3. profile 사용
oprofile 등 .. 이건 복잡하니 찾아보세요 구글에서 찾음 자료 나옵니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.