#include <stdio.h>
#include <sys/time.h>
int f(int d)
{
int k;
int n; // 桁数
int zero; // ゼロ個数
int res; // 結果
// 桁数数えなど
k = 1;
for (n = 0; d > k; n++) {
k *= 10;
}
// 0以外をresにいれながら0を数える
res = zero = 0;
for (; n > 0; n--) {
k /= 10;
if (d >= k) {
res = res * 10 + d / k;
}
else {
zero++;
}
d %= k;
}
// 0を付け加える
for (; zero > 0; zero--) {
res *= 10;
}
// 終了
return res;
}
int main()
{
int v = 20010307; // 値
//int v = 1020304050; // 値
int ex = 100000; // 実行回数
int count = 222; // 計測回数
int res;
int i, j;
struct timeval s, e;
double sum=0; // 結果
printf("f(%d);を%d回実行する時間の計測を%d回し、平均時間を出す\n", v
, ex
, count
); sum = 0.0;
for (j = 0; j < count; j++) {
gettimeofday(&s, NULL);
for (i = 0; i < ex; i++) {
res = f(v);
}
gettimeofday(&e, NULL);
sum += (e.tv_sec - s.tv_sec) + (e.tv_usec - s.tv_usec) / 1000000.0;
}
printf("result = %d, time = %fms\n", res
, 1000.0 * sum
/ ((double)count
)); return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzeXMvdGltZS5oPgoKaW50IGYoaW50IGQpCnsKCWludCBrOwoJaW50IG47ICAgICAgICAgICAgICAgICAgICAgIC8vIOahgeaVsAoJaW50IHplcm87ICAgICAgICAgICAgICAgICAgIC8vIOOCvOODreWAi+aVsAoJaW50IHJlczsgICAgICAgICAgICAgICAgICAgIC8vIOe1kOaenAoKCS8vIOahgeaVsOaVsOOBiOOBquOBqQoJayA9IDE7Cglmb3IgKG4gPSAwOyBkID4gazsgbisrKSB7CgkJayAqPSAxMDsKCX0KCS8vIDDku6XlpJbjgpJyZXPjgavjgYTjgozjgarjgYzjgokw44KS5pWw44GI44KLCglyZXMgPSB6ZXJvID0gMDsKCWZvciAoOyBuID4gMDsgbi0tKSB7CgkJayAvPSAxMDsKCQlpZiAoZCA+PSBrKSB7CgkJCXJlcyA9IHJlcyAqIDEwIC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