php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28642 Disabling assertions does not affect performance
Submitted: 2004-06-05 16:05 UTC Modified: 2005-02-11 17:07 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:1 of 1 (100.0%)
Same Version:2 (200.0%)
Same OS:2 (200.0%)
From: am at andremeyer dot name Assigned:
Status: Not a bug Package: Performance problem
PHP Version: 5.0.3 OS: Windows XP
Private report: No CVE-ID: None
 [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.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-11 15:33 UTC] tony2001@php.net
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.
 [2005-02-11 15:46 UTC] am at andremeyer dot name
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.
 [2005-02-11 15:50 UTC] meyer at labeltools dot com
In the last paragraph, I meant

ini_set('assert.active', false);

of course. Sorry for the inconvenience.

Best regards
Andr? Meyer
labeltools GmbH
 [2005-02-11 17:07 UTC] tony2001@php.net
>Well, assertions are not really fast - actually, they are a 
>bottle neck.
you're probably kidding me.

>As you can see from this, about 50% of the execution time 
>is spent in the assertion!
yes.
500 thousands of function calls took about a second.
is it a bottle neck?
you're real-life application calls assert() billions of times?

again, I don't see any performance problem here.

change "assert" in your code to something else and try again - you'll see the very same results, even worse.
function call is rather expensive operation (even if the function just returns true).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 18:01:31 2024 UTC