| メイン |
処理時間計測用マクロの例
2010年3月20日 お仕事C言語で処理時間計測用マクロをつくってみました。
関数マクロやdefineについてはhttp://www.c-lang.org/define.htmlを見てください。
/*
clock_macro.c
処理時間計測用マクロの例
*/
#include <stdio.h> /* printf */
#include <stdlib.h> /* EXIT_SUCCESS */
#include <time.h> /* clock */
#include <math.h> /* sin */
/*
処理時間の計測をしたいコードと
ループ回数を指定すると
処理にかかったプロセッサ時間を表示する
*/
#define CALC_CLOCK(code, n) { \
int i; \
clock_t t0 = clock(); \
for (i=0;i<n;i++) { code; } \
printf("%d\n", clock() - t0); \
}
#
int main(int argc, char *argv[])
{
int x = 1;
CALC_CLOCK(x = sin(x), 10000000UL);
return EXIT_SUCCESS;
}
関数マクロやdefineについてはhttp://www.c-lang.org/define.htmlを見てください。
/*
clock_macro.c
処理時間計測用マクロの例
*/
#include <stdio.h> /* printf */
#include <stdlib.h> /* EXIT_SUCCESS */
#include <time.h> /* clock */
#include <math.h> /* sin */
/*
処理時間の計測をしたいコードと
ループ回数を指定すると
処理にかかったプロセッサ時間を表示する
*/
#define CALC_CLOCK(code, n) { \
int i; \
clock_t t0 = clock(); \
for (i=0;i<n;i++) { code; } \
printf("%d\n", clock() - t0); \
}
#
int main(int argc, char *argv[])
{
int x = 1;
CALC_CLOCK(x = sin(x), 10000000UL);
return EXIT_SUCCESS;
}
| メイン |
コメント