1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C言語 関数のおおよその実行時間を表示させるコード

Last updated at Posted at 2020-03-15

コマンドを使って実行する方法もあるけれど、
__おおよその実行時間__を出すコード。
あくまで大体の時間なので、目安として使う時だけ。

# include <stdio.h>
# include <time.h>
//時間計測
clock_t cpu_time_start;
clock_t cpu_time_end;
double sec_start;
double sec_end;
double result_time;

int	main(void)
{
    //計測開始
    cpu_time_start = clock();
    sec_start = (double)cpu_time_start/CLOCKS_PER_SEC;
    printf("開始時間 : %f\n\n",sec_start);

    // ここに処理を書く

    //計測終了
    cpu_time_end = clock();
    sec_end = (double)cpu_time_end/CLOCKS_PER_SEC;
    printf("\n終了時間 : %f\n",sec_end);
    //処理時間
    result_time = sec_end - sec_start;
    printf("処理時間 : %f\n",result_time);

    return (0);
}


2020/04/19 追記

関数をひとつずつ調べない場合はこっちの書き方の方でOK。

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

int main(void)
{
    // ここに処理を書く

    printf("%f秒\n", ((double)clock()) / CLOCKS_PER_SEC);
    return 0;
}
1
0
1

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?