fork(2) download
  1. <?php
  2. $out = fopen('php://stdout', "w");
  3. //
  4. function test() {
  5. // setup code
  6. $remove_values = array();
  7. for ($x = 0; $x <= 20; $x++) $remove_values[] = rand(2, 14);
  8. $card1 = rand(2, 14);
  9. $card2 = rand(2, 14);
  10. $card3 = rand(2, 14);
  11. $card4 = rand(2, 14);
  12. $card5 = rand(2, 14);
  13. $card6 = rand(2, 14);
  14. $card7 = rand(2, 14);
  15. // Testing code
  16. //
  17. foreach($remove_values as $value) {
  18. if($value === $card1) {
  19. $card1 = 0;
  20. }
  21. if($value === $card2) {
  22. $card2 = 0;
  23. }
  24. if($value === $card3) {
  25. $card3 = 0;
  26. }
  27. if($value === $card4) {
  28. $card4 = 0;
  29. }
  30. if($value === $card5) {
  31. $card5 = 0;
  32. }
  33. if($value === $card6) {
  34. $card6 = 0;
  35. }
  36. if($value === $card7) {
  37. $card7 = 0;
  38. }
  39. }
  40. //
  41. //
  42. return ($card1 + $card2 + $card3 + $card4 + $card5 + $card6 + $card7);
  43. }
  44. function test2() {
  45. // setup code
  46. $remove_values = array();
  47. /* USE ASSOC. ARRAY */
  48. for ($x = 0; $x <= 20; $x++) {
  49. $r = rand(2, 14);
  50. $remove_values[$r] = $r;
  51. }
  52. $card1 = rand(2, 14);
  53. $card2 = rand(2, 14);
  54. $card3 = rand(2, 14);
  55. $card4 = rand(2, 14);
  56. $card5 = rand(2, 14);
  57. $card6 = rand(2, 14);
  58. $card7 = rand(2, 14);
  59. // Testing code
  60. //
  61. $card1 = (isset($remove_values[$card1]) - 1) & $card1;
  62. $card2 = (isset($remove_values[$card2]) - 1) & $card2;
  63. $card3 = (isset($remove_values[$card3]) - 1) & $card3;
  64. $card4 = (isset($remove_values[$card4]) - 1) & $card4;
  65. $card5 = (isset($remove_values[$card5]) - 1) & $card5;
  66. $card6 = (isset($remove_values[$card6]) - 1) & $card6;
  67. $card7 = (isset($remove_values[$card7]) - 1) & $card7;
  68. //
  69. //
  70. return ($card1 + $card2 + $card3 + $card4 + $card5 + $card6 + $card7);
  71. }
  72. //
  73. $N = 30000;
  74.  
  75. // TEST ORIGINAL
  76. $total = 0;
  77. $start = microtime(true);
  78. for($x = 0; $x < $N; $x++) {
  79. srand($x + 1337);
  80. $total += test();
  81. }
  82. $end = microtime(true);
  83. fwrite($out, sprintf("Original\nTime %f\nResult %d\n", $end - $start, $total));
  84. // TEST OTHER
  85. $total = 0;
  86. $start = microtime(true);
  87. for($x = 0; $x < $N; $x++) {
  88. srand($x + 1337);
  89. $total += test2();
  90. }
  91. $end = microtime(true);
  92. fwrite($out, sprintf("Other\nTime %f\nResult %d\n", $end - $start, $total));
  93. // TEST ORIGINAL
  94. $total = 0;
  95. $start = microtime(true);
  96. for($x = 0; $x < $N; $x++) {
  97. srand($x + 1337);
  98. $total += test();
  99. }
  100. $end = microtime(true);
  101. fwrite($out, sprintf("Original\nTime %f\nResult %d\n", $end - $start, $total));
  102. // TEST OTHER
  103. $total = 0;
  104. $start = microtime(true);
  105. for($x = 0; $x < $N; $x++) {
  106. srand($x + 1337);
  107. $total += test2();
  108. }
  109. $end = microtime(true);
  110. fwrite($out, sprintf("Other\nTime %f\nResult %d\n", $end - $start, $total));
  111.  
  112. $v = 13;
  113. fwrite($out, "T ".(true - 1)." F ".(false - 1)."\n");
  114.  
  115. //
  116. fclose($out);
Success #stdin #stdout 2.93s 52480KB
stdin
Standard input is empty
stdout
Original
Time 0.831925
Result 310404
Other
Time 0.612388
Result 310404
Original
Time 0.840669
Result 310404
Other
Time 0.611303
Result 310404
T 0 F -1