IF
statement, and stopped to think about the performance implications of using various data types, or the difference between using the double equals ==
vs triple equals ===
. I have, and I decided to test this using the following basic code:
<?php $start = microtime(true); $data = true; for ($x = 0; $x < 100000000; $x++) { if ($data){ // do nothing } } $end = microtime(true); echo $end - $start; ?>Throughout each of the tests, I changed two things, the value of the
$data
variable, and the form of the if
statement. Each time I modified the code, I ran it at least 10 times, removed the outliers (occasionally the time would spike due to other processes on the computer running) and averaged them together. Here are the results:
Test 1
$data = true
if ($data)
Result: 4.1756498813629
Test 2
$data = 1
if($data)
Result: 4.1976277828217
Test 3
$data = 'a'
if($data)
Result: 4.0583989620209
Test 4
$data = 'a'
if ($data == 'a')
Result: 7.0086648464203
Test 5
$data = 'a'
if ($data === 'a')
Result: 5.226686000824
Test 6
$data = '1'
if ($data == 1)
Result: 11.350320100784
Test 7
$data = 1
if ($data == '1')
Result: 11.306849002838
Test 8
$data = 11111
if ($data === 11111)
Result: 4.8336350917816
Test 9
$data = 11111
if ($data === '11111')
Result: 4.3648190498352 Note that this evaluates to
false
Test 10
$data = 11111
if ($data == '11111')
Result: 14.024300098419
Test 11
$data = 9999999999
if ($data == '9999999999')
Result: 17.022763967514
Test 12
$data = 9999999999
if ($data == 9999999999)
Result: 4.6356329917908
Test 13
$data = 9999999999
if ($data === 9999999999)
Result: 4.7124888896942
Test 14
$data = true
if ($data == 'true')
Result: 5.8072018623352
Test 15
$data = true
if ($data == '1')
Result: 5.8963930606842
Test 16
$data = true
if ($data == 1)
Result: 5.8389239311218
Test 17
$data = 'This is a sentence'
if ($data == 'This is a sentence')
Result: 6.9975271224976
Test 18
$data = 'This is a sentence'
if ($data === 'This is a sentence')
Result: 6.4179918766022
Test 19
$data = '1'
if(intval($data) === 1
Result: 18.751703023911
Test 20
$data = true
if($data === true)
Result: 4.7625348567963
Conclusion
- Using the triple equals will typically run faster than double equals
- With double equals, comparing different data types will take longer than comparing the same data type
- When the data is only 1 character, the eval time difference is negligible between the data types
- Larger numbers make a slight impact, but not much
- An 18 character string takes about 1.5 times longer to run than a 1 character string
- Comparing 10 character, dissimilar data types takes about 4 times longer to run than the same data type
- It does NOT pay off to try converting the data types to be the same prior to the evaluation, using
intval
with triple equals (to compare two numbers) took about 1.5 times longer than trying to compare a string against a number with double equals - Testing if something is true without any equals signs (as in Test 1) runs faster than either two or three equals signs