howto/C言語

一行メモ


intやdoubleのデータ表現

nowrap

時間計測

#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相当など
#  など

int, longなどのサイズ

C99言語規格LP64データモデルLLP64データモデル
char1byte以上、int以下----
int2byte以上4byte4byte
long4byte以上、long long以下8byte4byte
long long8byte以上----
ポインタ--8byte8byte
実装例--LinuxなどWindowsなど

ref. [1] C++言語でのint型とlong型とlong long型の違いについて [2] cppreference.com 基本型

整数のオーバーフローについて知っておく事柄

多次元の配列とポインタとメモリ領域

nowrap

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS