fork(3) download
  1. // http://t...content-available-to-author-only...h.net/test/read.cgi/tech/1354393458/861
  2.  
  3. #include <stdio.h>
  4. #include <sys/time.h>
  5.  
  6. int f(int d)
  7. {
  8. int k;
  9. int n; // 桁数
  10. int zero; // ゼロ個数
  11. int res; // 結果
  12.  
  13. // check
  14. if (d < 10) {
  15. return d;
  16. }
  17. // 桁数数えなど
  18. k = 1;
  19. for (n = 0; d >= k; n++) {
  20. k *= 10;
  21. }
  22. k /= 10;
  23.  
  24. // 0以外をresにいれながら0を数える
  25. res = zero = 0;
  26. for (; n > 0; n--) {
  27. if (d >= k) {
  28. res = res * 10 + d / k;
  29. } else {
  30. zero++;
  31. }
  32. d %= k;
  33. k /= 10;
  34. }
  35.  
  36. // 0を付け加える
  37. for (; zero > 0; zero--) {
  38. res *= 10;
  39. }
  40.  
  41. // 終了
  42. return res;
  43. }
  44.  
  45. int main()
  46. {
  47. int v = 20010307; // 値
  48. // int v = 1020304050; // 値
  49. int ex = 100000; // 実行回数
  50. int count = 222; // 計測回数
  51. int res;
  52. int i, j;
  53. struct timeval s, e;
  54. double sum; // 結果
  55.  
  56. printf("f(%d);を%d回実行する時間の計測を%d回し、平均時間を出す\n", v, ex, count);
  57. sum = 0.0;
  58. for (j = 0; j < count; j++) {
  59. gettimeofday(&s, NULL);
  60. for (i = 0; i < ex; i++) {
  61. res = f(v);
  62. }
  63. gettimeofday(&e, NULL);
  64. sum += (e.tv_sec - s.tv_sec) + (e.tv_usec - s.tv_usec) / 1000000.0;
  65. }
  66. printf("result = %d, time = %fms\n", res, 1000.0 * sum / ((double) count));
  67.  
  68. return 0;
  69. }
  70.  
Success #stdin #stdout 0s 2248KB
stdin
Standard input is empty
stdout
f(20010307);を100000回実行する時間の計測を222回し、平均時間を出す
result = 21370000, time = 0.000171ms