// http://t...content-available-to-author-only...h.net/test/read.cgi/tech/1354393458/861
#include <stdio.h>
#include <sys/time.h>
int f(int d)
{
int k;
int n; // 桁数
int zero; // ゼロ個数
int res; // 結果
// check
if (d < 10) {
return d;
}
// 桁数数えなど
k = 1;
for (n = 0; d >= k; n++) {
k *= 10;
}
k /= 10;
// 0以外をresにいれながら0を数える
res = zero = 0;
for (; n > 0; n--) {
if (d >= k) {
res = res * 10 + d / k;
} else {
zero++;
}
d %= k;
k /= 10;
}
// 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; // 結果
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;
}
Ly8gaHR0cDovL3QuLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLmgubmV0L3Rlc3QvcmVhZC5jZ2kvdGVjaC8xMzU0MzkzNDU4Lzg2MQoKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzeXMvdGltZS5oPgoKaW50IGYoaW50IGQpCnsKICAgIGludCBrOwogICAgaW50IG47ICAgICAgICAgICAgICAgICAgICAgIC8vIOahgeaVsAogICAgaW50IHplcm87ICAgICAgICAgICAgICAgICAgIC8vIOOCvOODreWAi+aVsAogICAgaW50IHJlczsgICAgICAgICAgICAgICAgICAgIC8vIOe1kOaenAoKICAgIC8vIGNoZWNrCiAgICBpZiAoZCA8IDEwKSB7CiAgICAgICAgcmV0dXJuIGQ7CiAgICB9CiAgICAvLyDmoYHmlbDmlbDjgYjjgarjgakKICAgIGsgPSAxOwogICAgZm9yIChuID0gMDsgZCA+PSBrOyBuKyspIHsKICAgICAgICBrICo9IDEwOwogICAgfQogICAgayAvPSAxMDsKCiAgICAvLyAw5Lul5aSW44KScmVz44Gr44GE44KM44Gq44GM44KJMOOCkuaVsOOBiOOCiwogICAgcmVzID0gemVybyA9IDA7CiAgICBmb3IgKDsgbiA+IDA7IG4tLSkgewogICAgICAgIGlmIChkID49IGspIHsKICAgICAgICAgICAgcmVzID0gcmVzICogMTAgKyBkIC8gazsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICB6ZXJvKys7CiAgICAgICAgfQogICAgICAgIGQgJT0gazsKICAgICAgICBrIC89IDEwOwogICAgfQoKICAgIC8vIDDjgpLku5jjgZHliqDjgYjjgosKICAgIGZvciAoOyB6ZXJvID4gMDsgemVyby0tKSB7CiAgICAgICAgcmVzICo9IDEwOwogICAgfQoKICAgIC8vIOe1guS6hgogICAgcmV0dXJuIHJlczsKfQoKaW50IG1haW4oKQp7CiAgICBpbnQgdiA9IDIwMDEwMzA3OyAgICAgICAgICAgLy8g5YCkCi8vICAgIGludCB2ID0gMTAyMDMwNDA1MDsgICAgICAgICAvLyDlgKQKICAgIGludCBleCA9IDEwMDAwMDsgICAgICAgICAgICAvLyDlrp/ooYzlm57mlbAKICAgIGludCBjb3VudCA9IDIyMjsgICAgICAgICAgICAvLyDoqIjmuKzlm57mlbAKICAgIGludCByZXM7CiAgICBpbnQgaSwgajsKICAgIHN0cnVjdCB0aW1ldmFsIHMsIGU7CiAgICBkb3VibGUgc3VtOyAgICAgICAgICAgICAgICAgLy8g57WQ5p6cCgogICAgcHJpbnRmKCJmKCVkKTvjgpIlZOWbnuWun+ihjOOBmeOCi+aZgumWk+OBruioiOa4rOOCkiVk5Zue44GX44CB5bmz5Z2H5pmC6ZaT44KS5Ye644GZXG4iLCB2LCBleCwgY291bnQpOwogICAgc3VtID0gMC4wOwogICAgZm9yIChqID0gMDsgaiA8IGNvdW50OyBqKyspIHsKICAgICAgICBnZXR0aW1lb2ZkYXkoJnMsIE5VTEwpOwogICAgICAgIGZvciAoaSA9IDA7IGkgPCBleDsgaSsrKSB7CiAgICAgICAgICAgIHJlcyA9IGYodik7CiAgICAgICAgfQogICAgICAgIGdldHRpbWVvZmRheSgmZSwgTlVMTCk7CiAgICAgICAgc3VtICs9IChlLnR2X3NlYyAtIHMudHZfc2VjKSArIChlLnR2X3VzZWMgLSBzLnR2X3VzZWMpIC8gMTAwMDAwMC4wOwogICAgfQogICAgcHJpbnRmKCJyZXN1bHQgPSAlZCwgdGltZSA9ICVmbXNcbiIsIHJlcywgMTAwMC4wICogc3VtIC8gKChkb3VibGUpIGNvdW50KSk7CgogICAgcmV0dXJuIDA7Cn0K