C90規格(-std=c90)だと、double myfunc( int i, struct hoge* g ) があるとき、 この関数を前方宣言しないまま使うと、コンパイラは int myfunc(int i, ...) のような int型を返す、可変引数の関数として扱われるようで、警告やエラーは出さない。 C99規格では警告してくれる。 myfunc(int i, ...)を myfunc( int i, struct hoge* g )と思って作っていると、 実行結果は実に不可解な結果を与える。
#include <sys/time.h>
struct timeval ts, te;
gettimeofday( &ts, NULL );
...
gettimeofday( &te, NULL );
printf( "gettimeofday: %.6lf sec\n",
te.tv_sec - ts.tv_sec + ( te.tv_usec - ts.tv_usec ) / 1000000.0 );
//usec精度、NTPなどの時刻変更の影響を受けうる
#include <time.h>
time_t ts, te;
ts = time( NULL );
...
te = time( NULL );
printf( "time: %d sec\n", (int)(te-ts) );
//sec精度
#include <time.h>
clock_t ts, te;
ts = clock();
...
te = clock();
printf( "clock: %.6lf sec\n", (double)( te - ts ) / CLOCKS_PER_SEC );
//msec以下の精度、I/O待ちなどは含まれない、子プロセスの時間も含まれる
#include <time.h>
//#define CLKID CLOCK_REALTIME
#define CLKID CLOCK_MONOTONIC_RAW //詳しくはman
struct timespec ts, te, tr;
clock_gettime( CLKID, &ts );
...
clock_gettime( CLKID, &te );
printf( "clock_gettime: %.9lf sec\n",
(double)( te.tv_sec - ts.tv_sec ) + (double)( te.tv_nsec - ts.tv_nsec )/1000000000 );
//nsec精度、もっとも正確らしい、
//CLOCK_MONOTONIC_RAW はNTPなどの時刻変更の影響は受けない、
//CLOCK_REALTIME は実時間でありNTPなどの影響を受ける
clock_getres( CLKID, &tr );
printf( " resolution: %lf sec %lf nsec\n",
(double)tr.tv_sec, (double)tr.tv_nsec ); //CLKIDの解像度を表示
[cf] python
import time #詳しくはマニュアルhttps://docs.python.org/
ts = time.time()
...
te = time.time()
print( f"time.time: {te-ts:.3f} sec" )
#処理系によってはsec精度まで
#他)
# time.perf_counter(): もっとも正確らしい、clock_gettime相当らしい
# time.process_time(): スリープ時間は含まず
# time.monotonic(): OS依存だがclock_gettime(CLOCK_MONOTONIC)相当など
# time.time(): OS依存だがgettimeofday相当など
# など
| C99言語規格 | LP64データモデル | LLP64データモデル | |
| char | 1byte以上、int以下 | -- | -- |
| int | 2byte以上 | 4byte | 4byte |
| long | 4byte以上、long long以下 | 8byte | 4byte |
| long long | 8byte以上 | -- | -- |
| ポインタ | -- | 8byte | 8byte |
| 実装例 | -- | Linuxなど | Windowsなど |
ref. [1] C++言語でのint型とlong型とlong long型の違いについて [2] cppreference.com 基本型