|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-06-05 16:05 UTC] am at andremeyer dot name
Description:
------------
Disabling assertions in PHP does not affect performance. After testing my software with assertions, I want to disable assertions. I do this with
ini_set('assert.active', false);
Surprisingly, the assert() statements in my software _still_ consume a considerable amount of time.
Reproduce code:
---------------
<?php
$doAsserts = array(true, false);
foreach ($doAsserts as $doAssert)
{
ini_set('assert.active', $doAssert); // Turn assertions on/off
$startTime = microtime(true);
for ($i = 0; $i < 1000000; $i++)
assert(is_numeric($i));
$endTime = microtime(1);
$time = $endTime - $startTime;
print "Elapsed time: $time\r\n";
}
?>
Expected result:
----------------
I expect something like:
Elapsed time: 1.3514750003815 // assertions turned on
Elapsed time: 0.4243453254354 // assertions turned off
I expect the elapsed time to decrease when turning assertions off.
Actual result:
--------------
Elapsed time: 1.3480539321899 // assertions turned on
Elapsed time: 1.3210921287537 // assertions turned off
As you can see, when turning assertions off, the elapsed time is nearly the same. So I have to comment all my assertions in order to achieve a better peformance.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 29 19:00:01 2025 UTC |
So, that means that assertions are so fast, that disabling them doesn't affect script performance. Agree? But if you change your code in this way: assert(is_numeric($i)); => assert("is_numeric($i)"); you'll get expected results.Well, assertions are not really fast - actually, they are a bottle neck. I modified my previous script. Look at this: ----------------------------------------------------------------------------- <?php $doAsserts = array(true, false); foreach ($doAsserts as $doAssert) { ini_set('assert.active', $doAssert); // Turn assertions on/off $startTime = microtime(true); for ($i = -500000; $i < 500000; $i++) { $result = $i * $i; // just to have assert($result >= 0); } $endTime = microtime(1); $time = $endTime - $startTime; print "Elapsed time: $time\r\n"; } ?> ----------------------------------------------------------------------------- Running this script, I get: Elapsed time: 1.6357040405273 Elapsed time: 1.6131460666656 Now I comment the line "assert($result >= 0);" so that the script looks like this: ----------------------------------------------------------------------------- <?php $doAsserts = array(true, false); foreach ($doAsserts as $doAssert) { ini_set('assert.active', $doAssert); // Turn assertions on/off $startTime = microtime(true); for ($i = -500000; $i < 500000; $i++) { $result = $i * $i; // just to have //assert($result >= 0); } $endTime = microtime(1); $time = $endTime - $startTime; print "Elapsed time: $time\r\n"; } ?> ----------------------------------------------------------------------------- When I run the script again, I get: Elapsed time: 0.87688493728638 Elapsed time: 0.87343096733093 As you can see from this, about 50% of the execution time is spent in the assertion! This would be still acceptable, if I could simply disable assertions. Well, I can - by ini_set('assert.active', true); - however, this only disables assertions without affecting the performance at all. When disabling assertions, any assertion should rather be considered as not existant (like a comment). I hope this can be fixed in a future PHP version! Thanks.In the last paragraph, I meant ini_set('assert.active', false); of course. Sorry for the inconvenience. Best regards Andr? Meyer labeltools GmbH