Help us understand the problem. What is going on with this article?

プログラムの経過実時間をナノ秒単位で出力(C, Linux)

More than 1 year has passed since last update.

コード

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

int main(void) {
  struct timespec startTime, endTime, sleepTime;

  clock_gettime(CLOCK_REALTIME, &startTime);
  sleepTime.tv_sec = 0;
  sleepTime.tv_nsec = 123;

  // 何らかの処理を実行
  for (int ii = 0; ii < 1000; ii++) {
    // 何らかの処理
    nanosleep(&sleepTime, NULL);
  }
  clock_gettime(CLOCK_REALTIME, &endTime);

  // 処理時間を出力
  printf("開始時刻  = %10ld.%09ld\n", startTime.tv_sec, startTime.tv_nsec);
  printf("終了時刻  = %10ld.%09ld\n", endTime.tv_sec, endTime.tv_nsec);
  printf("経過実時間 = ");
  if (endTime.tv_nsec < startTime.tv_nsec) {
    printf("%10ld.%09ld", endTime.tv_sec - startTime.tv_sec - 1
          ,endTime.tv_nsec + 1000000000 - startTime.tv_nsec);
  } else {
    printf("%10ld.%09ld", endTime.tv_sec - startTime.tv_sec
          ,endTime.tv_nsec - startTime.tv_nsec);
  }
  printf("(秒)\n");

  return 0;
}

出力結果

開始時刻  = 1477137993.077370573
終了時刻  = 1477137993.694312968
経過実時間 =          0.616942395(秒)

 clock_gettime()関数から得られるタイムスタンプをそのままExcelに載せようとすると、表示桁数の限界(15桁)という壁にぶち当たるんですよね。
 Excelで結果を整形したい気持ちがありましたが、仕方なくプログラムのほうで整形しました。
 Excelの最大表示桁数30とかにならないかな……。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away