0
0

時刻計測

Posted at

ftime()関数を<sys/time.h>gettimeofday()関数で代替する場合、次のようなコードを使用できます。

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

int main() {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    printf("Seconds since epoch: %ld\n", tv.tv_sec);
    printf("Microseconds: %ld\n", tv.tv_usec);
    return 0;
}

このコードは、現在の時間を秒単位で取得し、エポックからの経過秒数とマイクロ秒単位の時間を表示します。

もし、精度の高い時間情報が必要な場合は、<time.h>clock_gettime()関数を使用できます。

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

int main() {
    struct timespec ts;
    clock_gettime(CLOCK_REALTIME, &ts);
    printf("Seconds since epoch: %ld\n", ts.tv_sec);
    printf("Nanoseconds: %ld\n", ts.tv_nsec);
    return 0;
}

このコードは、CLOCK_REALTIMEを使用して、実時間の時間情報を取得します。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0