fork(12) download
  1. #include "time.h"
  2. #include "math.h"
  3. #include "assert.h"
  4.  
  5. int num_tests = 5000000;
  6.  
  7. unsigned duck(unsigned x, unsigned y) {
  8. unsigned pow = 10;
  9. while(y >= pow)
  10. pow *= 10;
  11. return x * pow + y;
  12. }
  13. int myPow(int x, int p)
  14. {
  15. if (p == 0) return 1;
  16. if (p == 1) return x;
  17.  
  18. int tmp = myPow(x, p/2);
  19. if (p%2 == 0) return tmp * tmp;
  20. else return x * tmp * tmp;
  21. }
  22. int david(int x, int y) {
  23. int power = log10(y);
  24. return x*myPow(10,power+1)+y;
  25. }
  26.  
  27. int drummer(int x, int y) {
  28. int digits = log10(y)+1;
  29. int shifted = x * pow(10, digits); // will be 1100 in your example
  30. return shifted + y; // 1111
  31. }
  32.  
  33. int shabeer(int x, int y) {
  34. int temp=0;
  35. int z=x;
  36. while(y>0)
  37. {
  38. // take reciprocal of y into temp
  39. temp=(temp*10)+(y%10);
  40. y=y/10;
  41. }
  42. while(temp>0)
  43. {
  44. // take each number from last of temp and add to last of z
  45. z=(z*10)+(temp%10);
  46. temp=temp/10;
  47. }
  48. return z;
  49. }
  50.  
  51. int gokcehan(int x, int y) {
  52. int temp = y;
  53. while (y != 0) {
  54. x *= 10;
  55. y /= 10;
  56. }
  57. return x + temp;
  58. }
  59.  
  60. void int_time(const char* name, unsigned(*func)(unsigned, unsigned)) {
  61. clock_t start = clock();
  62. unsigned r = 0;
  63. unsigned i = 0;
  64. for(; i<num_tests; ++i)
  65. r += func(i, i);
  66. printf("%s found %u in %d\n", name, r, clock()-start);
  67. }
  68.  
  69. void uint_time(const char* name, int(*func)(int, int)) {
  70. clock_t start = clock();
  71. unsigned r = 0;
  72. unsigned i = 0;
  73. for(; i<num_tests; ++i)
  74. r += func(i, i);
  75. printf("%s found %u in %d\n", name, r, clock()-start);
  76. }
  77.  
  78. void uint_accuracy(const char* name, unsigned(*func)(unsigned, unsigned)) {
  79. printf("testing %s accuracy: ", name);
  80. assert(func(0, 0) == 0);
  81. assert(func(0, 1) == 1);
  82. assert(func(0, 10) == 10);
  83. assert(func(1, 0) == 10);
  84. assert(func(1, 1) == 11);
  85. assert(func(1, 10) == 110);
  86. assert(func(10, 0) == 100);
  87. assert(func(10, 1) == 101);
  88. assert(func(10, 10) == 1010);
  89. assert(func(23, 456) == 23456);
  90. assert(func(234, 56) == 23456);
  91. printf("pass\n");
  92. }
  93.  
  94. void int_accuracy(const char* name, int(*func)(int, int)) {
  95. printf("testing %s accuracy: ", name);
  96. assert(func(0, 0) == 0);
  97. assert(func(0, 1) == 1);
  98. assert(func(0, 10) == 10);
  99. assert(func(1, 0) == 10); //several fail this one
  100. assert(func(1, 1) == 11);
  101. assert(func(1, 10) == 110);
  102. assert(func(10, 0) == 100); //several fail this one
  103. assert(func(10, 1) == 101);
  104. assert(func(10, 10) == 1010);
  105. assert(func(23, 456) == 23456);
  106. assert(func(234, 56) == 23456);
  107. printf("pass\n");
  108. }
  109.  
  110. int main() {
  111. uint_time("duck", duck);
  112. uint_time("duck", duck);
  113. int_time("david", david);
  114. int_time("david", david);
  115. int_time("drummer", drummer);
  116. int_time("drummer", drummer);
  117. int_time("shabeer", shabeer);
  118. int_time("shabeer", shabeer);
  119. int_time("gokcehan", gokcehan);
  120. int_time("gokcehan", gokcehan);
  121. uint_accuracy("duck", duck);
  122. //int_accuracy("gokcehan", gokcehan); //Assertion `func(1, 0) == 10' failed.
  123. //int_accuracy("drummer", drummer); //Assertion `func(1, 0) == 10' failed.
  124. //int_accuracy("david", david); //Assertion `func(1, 0) == 10' failed.
  125. //int_accuracy("shabeer", shabeer); //Assertion `func(0, 10) == 10' failed.
  126. return 0;
  127. }
Success #stdin #stdout 4.18s 1680KB
stdin
Standard input is empty
stdout
duck found 2343525334 in 40000
duck found 2343525334 in 40000
david found 2343525334 in 710000
david found 2343525334 in 680000
drummer found 3188974518 in 950000
drummer found 3188974518 in 930000
shabeer found 2299299461 in 290000
shabeer found 2299299461 in 280000
gokcehan found 2343525334 in 120000
gokcehan found 2343525334 in 130000
testing duck accuracy: pass