| 1 | <pre> |
|---|
| 2 | <?php |
|---|
| 3 | /* |
|---|
| 4 | * intval and casting integers tests |
|---|
| 5 | * |
|---|
| 6 | * Last Updated: 13. May 2010 |
|---|
| 7 | * |
|---|
| 8 | * @author hakre <http://hakre.wordpress.com/> |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | $values = array( |
|---|
| 12 | array('value' => 42 , 'intval' => 42), |
|---|
| 13 | array('value' => -42 , 'intval' => -42), |
|---|
| 14 | array('value' => 4.2 , 'intval' => 4 ), |
|---|
| 15 | array('value' => '42' , 'intval' => 42 ), |
|---|
| 16 | array('value' => '+42' , 'intval' => 42 ), |
|---|
| 17 | array('value' => '-42' , 'intval' => -42), |
|---|
| 18 | array('value' => 042 , 'intval' => 34 , 'notation' => '042' ), # 34 |
|---|
| 19 | array('value' => '042' , 'intval' => 42 ), |
|---|
| 20 | array('value' => 1e10 , 'intval' => 1410065408, 'notation' => '1e10'), # 10000000000 |
|---|
| 21 | array('value' => '1e10' , 'intval' => 1 ), |
|---|
| 22 | array('value' => 0x1A , 'intval' => 26 , 'notation' => '0x1A'), # 26 |
|---|
| 23 | array('value' => 42000000 , 'intval' => 42000000), |
|---|
| 24 | array('value' => 2147483647 , 'intval' => 2147483647), |
|---|
| 25 | array('value' => 2147483648 , 'intval' => -2147483648), |
|---|
| 26 | array('value' => 420000000000000000000 , 'intval' => 0 , 'notation' => '420000000000000000000'), # 4.2E+20 |
|---|
| 27 | array('value' => '420000000000000000000', 'intval' => 2147483647), |
|---|
| 28 | array('value' => array() , 'intval' => 0 ), |
|---|
| 29 | array('value' => array('foo', 'bar') , 'intval' => 1 ), |
|---|
| 30 | ); |
|---|
| 31 | |
|---|
| 32 | $tests = array_keys($values); |
|---|
| 33 | $labels = array(0 => 'pass.', 1 => 'failed.', 2 => 'different.', 3 => 'error.' , 4 => 'skipped.'); |
|---|
| 34 | |
|---|
| 35 | printf ("PHP %s %s/%s - %s\n\n", PHP_VERSION, PHP_OS, PHP_SAPI, str_replace(' +0000', ' UTC ', gmdate('r'))); |
|---|
| 36 | |
|---|
| 37 | print "\n* TEST #1: Comparison of results intval() and casting to integer:\n\n"; |
|---|
| 38 | print "# INPUT EXPECTED INTVAL() CAST RESULT\n"; |
|---|
| 39 | print "----------------------------------------------------------------------------\n"; |
|---|
| 40 | foreach ($tests as $test) { |
|---|
| 41 | flush(); |
|---|
| 42 | $value = $values[$test]; |
|---|
| 43 | $input = $value['value']; |
|---|
| 44 | $notation = test_varnotation($value); |
|---|
| 45 | $expected = $value['intval']; |
|---|
| 46 | $intval = intval($input); |
|---|
| 47 | $castval = (int) $input; |
|---|
| 48 | $result = 1 - (int) ($expected == $intval && $intval === $castval); |
|---|
| 49 | $label = $labels[$result]; |
|---|
| 50 | printf('#%-2s %-23s : %-11s / %-11s / %-11s %s', $test, $notation, $expected, $intval, $castval, $label); |
|---|
| 51 | |
|---|
| 52 | print "\n"; |
|---|
| 53 | } |
|---|
| 54 | print "\n"; |
|---|
| 55 | flush(); |
|---|
| 56 | |
|---|
| 57 | print "\n* TEST #2: Comparison of results intval() and 0 + \$var:\n\n"; |
|---|
| 58 | print "# INPUT EXPECTED INTVAL() 0+\$VAR RESULT\n"; |
|---|
| 59 | print "----------------------------------------------------------------------------\n"; |
|---|
| 60 | foreach ($tests as $test) { |
|---|
| 61 | flush(); |
|---|
| 62 | $value = $values[$test]; |
|---|
| 63 | $input = $value['value']; |
|---|
| 64 | $notation = test_varnotation($value); |
|---|
| 65 | $expected = $value['intval']; |
|---|
| 66 | $intval = intval($input); |
|---|
| 67 | if (is_array($input)) { |
|---|
| 68 | $castval = $labels[3]; |
|---|
| 69 | $label = $labels[4]; |
|---|
| 70 | } else { |
|---|
| 71 | $castval = 0 + $input; |
|---|
| 72 | $result = 1 - (int) ($expected == $intval && $intval === $castval); |
|---|
| 73 | $label = $labels[$result * 2]; |
|---|
| 74 | } |
|---|
| 75 | printf('#%-2s %-23s : %-11s / %-11s / %-11s %s', $test, $notation, $expected, $intval, $castval, $label); |
|---|
| 76 | |
|---|
| 77 | print "\n"; |
|---|
| 78 | } |
|---|
| 79 | print "\n"; |
|---|
| 80 | flush(); |
|---|
| 81 | |
|---|
| 82 | print "\n* TEST #3: abs((int)\$val) vs. (int) abs(\$val) \n\n"; |
|---|
| 83 | print "# INPUT ABS((INT)\$val) (INT)ABS(\$val) RESULT\n"; |
|---|
| 84 | print "----------------------------------------------------------------------------\n"; |
|---|
| 85 | foreach ($tests as $test) { |
|---|
| 86 | flush(); |
|---|
| 87 | $value = $values[$test]; |
|---|
| 88 | $input = $value['value']; |
|---|
| 89 | $notation = test_varnotation($value); |
|---|
| 90 | $absint = abs((int) $input); |
|---|
| 91 | $intabs = (int) abs($input); |
|---|
| 92 | $result = 1 - (int) ($absint === $intabs); |
|---|
| 93 | $label = $labels[$result * 2]; |
|---|
| 94 | printf('#%-2s %-23s : %-14s / %-11s %s', $test, $notation, $absint, $intabs, $label); |
|---|
| 95 | print "\n"; |
|---|
| 96 | } |
|---|
| 97 | print "\n"; |
|---|
| 98 | flush(); |
|---|
| 99 | |
|---|
| 100 | print "\n* TEST #4: abs((int)\$val) vs. absint(\$val) \n\n"; |
|---|
| 101 | print "# INPUT ABS((INT)\$val) ABSINT(\$val) RESULT\n"; |
|---|
| 102 | print "----------------------------------------------------------------------------\n"; |
|---|
| 103 | foreach ($tests as $test) { |
|---|
| 104 | flush(); |
|---|
| 105 | $value = $values[$test]; |
|---|
| 106 | $input = $value['value']; |
|---|
| 107 | $notation = test_varnotation($value); |
|---|
| 108 | $absint = abs((int) $input); |
|---|
| 109 | $intabs = absint($input); |
|---|
| 110 | $result = 1 - (int) ($absint === $intabs); |
|---|
| 111 | $label = $labels[$result * 2]; |
|---|
| 112 | printf('#%-2s %-23s : %-14s / %-10s %s', $test, $notation, $absint, $intabs, $label); |
|---|
| 113 | print "\n"; |
|---|
| 114 | } |
|---|
| 115 | print "\n"; |
|---|
| 116 | flush(); |
|---|
| 117 | |
|---|
| 118 | ini_set("max_execution_time", "300"); |
|---|
| 119 | $iterations = 100000; |
|---|
| 120 | |
|---|
| 121 | print "\n* TEST #5: Speed comparison (int)\$val vs. intval(\$val) \n\n"; |
|---|
| 122 | print "# INPUT (INT)\$val INTVAL(\$val) RATIO\n"; |
|---|
| 123 | print "----------------------------------------------------------------------------\n"; |
|---|
| 124 | foreach ($tests as $test) { |
|---|
| 125 | flush(); |
|---|
| 126 | $value = $values[$test]; |
|---|
| 127 | $input = $value['value']; |
|---|
| 128 | $notation = test_varnotation($value); |
|---|
| 129 | |
|---|
| 130 | $start = microtime(true); |
|---|
| 131 | for ($i = 0; $i < $iterations; $i++) { |
|---|
| 132 | $calc = (int) $input; |
|---|
| 133 | } |
|---|
| 134 | $stop = microtime(true); |
|---|
| 135 | $timea = $stop - $start; |
|---|
| 136 | |
|---|
| 137 | $start = microtime(true); |
|---|
| 138 | for ($i = 0; $i < $iterations; $i++) { |
|---|
| 139 | $calc = intval($input); |
|---|
| 140 | } |
|---|
| 141 | $stop = microtime(true); |
|---|
| 142 | $timeb = $stop - $start; |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | $ratio = ($timeb / $timea) * 100; |
|---|
| 146 | printf('#%-2s %-23s : %-12f / %-12f %3d%%', $test, $notation, $timea, $timeb, $ratio); |
|---|
| 147 | print "\n"; |
|---|
| 148 | } |
|---|
| 149 | printf("(%s iterations each)\n\n", number_format($iterations)); |
|---|
| 150 | flush(); |
|---|
| 151 | |
|---|
| 152 | print "\n* TEST #6: Speed comparison settype() vs. intval() \n\n"; |
|---|
| 153 | print "# INPUT SETTYPE() INTVAL(\$val) RATIO\n"; |
|---|
| 154 | print "----------------------------------------------------------------------------\n"; |
|---|
| 155 | foreach ($tests as $test) { |
|---|
| 156 | flush(); |
|---|
| 157 | $value = $values[$test]; |
|---|
| 158 | $input = $value['value']; |
|---|
| 159 | $notation = test_varnotation($value); |
|---|
| 160 | |
|---|
| 161 | $start = microtime(true); |
|---|
| 162 | for ($i = 0; $i < $iterations; $i++) { |
|---|
| 163 | $calc = $input; |
|---|
| 164 | $r = settype( $calc, 'int' ); |
|---|
| 165 | } |
|---|
| 166 | $stop = microtime(true); |
|---|
| 167 | $timea = $stop - $start; |
|---|
| 168 | |
|---|
| 169 | $start = microtime(true); |
|---|
| 170 | for ($i = 0; $i < $iterations; $i++) { |
|---|
| 171 | $calc = $input; |
|---|
| 172 | $calc = intval($input); |
|---|
| 173 | } |
|---|
| 174 | $stop = microtime(true); |
|---|
| 175 | $timeb = $stop - $start; |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | $ratio = ($timeb / $timea) * 100; |
|---|
| 179 | printf('#%-2s %-23s : %-12f / %-12f %3d%%', $test, $notation, $timea, $timeb, $ratio); |
|---|
| 180 | print "\n"; |
|---|
| 181 | } |
|---|
| 182 | printf("(%s iterations each)\n\n", number_format($iterations)); |
|---|
| 183 | flush(); |
|---|
| 184 | |
|---|
| 185 | print "\n* TEST #7: Speed comparison abs((int)\$val) vs. absint(\$val) \n\n"; |
|---|
| 186 | print "# INPUT ABS((INT)\$val) ABSINT(\$val) RATIO\n"; |
|---|
| 187 | print "----------------------------------------------------------------------------\n"; |
|---|
| 188 | foreach ($tests as $test) { |
|---|
| 189 | flush(); |
|---|
| 190 | $value = $values[$test]; |
|---|
| 191 | $input = $value['value']; |
|---|
| 192 | $notation = test_varnotation($value); |
|---|
| 193 | |
|---|
| 194 | $start = microtime(true); |
|---|
| 195 | for ($i = 0; $i < $iterations; $i++) { |
|---|
| 196 | $calc = abs((int) $input); |
|---|
| 197 | } |
|---|
| 198 | $stop = microtime(true); |
|---|
| 199 | $timea = $stop - $start; |
|---|
| 200 | |
|---|
| 201 | $start = microtime(true); |
|---|
| 202 | for ($i = 0; $i < $iterations; $i++) { |
|---|
| 203 | $calc = absint($input); |
|---|
| 204 | } |
|---|
| 205 | $stop = microtime(true); |
|---|
| 206 | $timeb = $stop - $start; |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | $ratio = ($timeb / $timea) * 100; |
|---|
| 210 | printf('#%-2s %-23s : %-12f / %-12f %3d%%', $test, $notation, $timea, $timeb, $ratio); |
|---|
| 211 | print "\n"; |
|---|
| 212 | } |
|---|
| 213 | printf("(%s iterations each)\n\n", number_format($iterations)); |
|---|
| 214 | flush(); |
|---|
| 215 | |
|---|
| 216 | print "\n* TEST #8: Speed comparison (int)\$val vs. 0 + \$val \n\n"; |
|---|
| 217 | print "# INPUT (INT)\$val 0+\$val RATIO\n"; |
|---|
| 218 | print "----------------------------------------------------------------------------\n"; |
|---|
| 219 | foreach ($tests as $test) { |
|---|
| 220 | flush(); |
|---|
| 221 | $value = $values[$test]; |
|---|
| 222 | $input = $value['value']; |
|---|
| 223 | $notation = test_varnotation($value); |
|---|
| 224 | |
|---|
| 225 | if (is_array($input)) { |
|---|
| 226 | printf('#%-2s %-23s : %s', $test, $notation, $labels[4]); |
|---|
| 227 | print "\n"; |
|---|
| 228 | continue; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | $start = microtime(true); |
|---|
| 232 | for ($i = 0; $i < $iterations; $i++) { |
|---|
| 233 | $calc = (int) $input; |
|---|
| 234 | } |
|---|
| 235 | $stop = microtime(true); |
|---|
| 236 | $timea = $stop - $start; |
|---|
| 237 | |
|---|
| 238 | $start = microtime(true); |
|---|
| 239 | for ($i = 0; $i < $iterations; $i++) { |
|---|
| 240 | $calc = 0 + $input; |
|---|
| 241 | } |
|---|
| 242 | $stop = microtime(true); |
|---|
| 243 | $timeb = $stop - $start; |
|---|
| 244 | |
|---|
| 245 | $ratio = ($timeb / $timea) * 100; |
|---|
| 246 | printf('#%-2s %-23s : %-12f / %-12f %3d%%', $test, $notation, $timea, $timeb, $ratio); |
|---|
| 247 | print "\n"; |
|---|
| 248 | } |
|---|
| 249 | printf("(%s iterations each)\n\n", number_format($iterations)); |
|---|
| 250 | flush(); |
|---|
| 251 | |
|---|
| 252 | print "</pre>\n"; |
|---|
| 253 | return; |
|---|
| 254 | |
|---|
| 255 | function test_varnotation(array $test) { |
|---|
| 256 | if (isset($test['notation'])) { |
|---|
| 257 | return $test['notation']; |
|---|
| 258 | } |
|---|
| 259 | $input = $test['value']; |
|---|
| 260 | |
|---|
| 261 | $notation = preg_replace('(\n|\s+)', ' ' , var_export($input, true)); |
|---|
| 262 | $notation = str_replace(array('array ( ',' 0 => ',' 1 => ',', )'), array('array(', '',' ',')'), $notation); |
|---|
| 263 | return $notation; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | function absint( $maybeint ) { |
|---|
| 267 | return abs( intval( $maybeint ) ); |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | # This is the last line in the fil ?> |
|---|