Re: Re: Conditional compilation
| From: | Micah Gersten | Date: | Wed, 20 Aug 2008 15:45:31 +0000 |
| Subject: | Re: Re: Conditional compilation | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-278407@lists.php.net to get a copy of this message | ||
You can always call a function with a DEBUG flag and execute certain
parts if it's set or not.
Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com
Robert Cummings wrote:
> On Fri, 2008-08-15 at 18:34 -0500, Shawn McKenzie wrote:
>
>> Herman Gomez wrote:
>>
>>> Hi,
>>>
>>> Here is something I used to do in C/C++ to include/exclude automaticaly
>>> all debugging code at compiling time:
>>>
>>> #define debug TRUE
>>> #ifdef(debug)
>>> //debugging code
>>> #endif
>>>
>>> That way I can include/exclude easily all debugging code in the final
>>> compiled code. In PHP I have not been able to find anything like that.
>>> The only solution I've found is having this kind of code in every debug
>>> code block:
>>>
>>> if ($debug) {
>>> //debugging code
>>> }
>>>
>>> But this means that the debugging code is in the final compiled
>>> (interpreted) code, wasting cpu cycles even if there won't be any
>>> debugging in production.
>>>
>>> Does somebody know if there is something like conditional compilation in
>>> PHP that I can use?
>>>
>>> Regards,
>>> Herman Gomez
>>> Madrid, Spain.
>>>
>> Well PHP isn't compiled it's interpreted. Still I don't see much diff
>> and no overhead between the following:
>>
>> #ifdef(debug)
>> //debugging code
>> #endif
>>
>> ---and---
>>
>> if (defined('DEBUG')) {
>> //debugging code
>> }
>>
>> I don't think checking a define is cpu intensive or even measurable.
>> You could "assume" that it's defined as true or false and:
>>
>> if (DEBUG === true)) {
>> //debugging code
>> }
>>
>> Still, I don't think that even checking $debug is measurable.
>>
>
> That depends on where the conditional exists. In C you can place it
> anywhere, including wihtin a tight loop. In PHP you end up having to
> either take an overhead penalty or duplicate code to force the
> conditional outside of a tight loop.
>
> Contrast the following:
>
> <?php
>
> if( DEBUG === true )
> {
> for( $i = 0; $i < 1000000; $i++ )
> {
> // Do something common between DEBUG and !DEBUG modes.
> // Do something dependent on debug mode.
> }
> }
> else
> {
> for( $i = 0; $i < 1000000; $i++ )
> {
> // Do something common between DEBUG and !DEBUG modes.
> }
> }
>
> ?>
>
> Versus:
>
> <?php
>
> for( $i = 0; $i < 1000000; $i++ )
> {
> // Do something common between DEBUG and !DEBUG modes.
>
> if( DEBUG === true )
> {
> // Do something dependent on debug mode.
> }
> }
>
> ?>
>
> Now depending on what "Do something common between DEBUG and !DEBUG
> modes" does, it can be a real PITA to do code duplication to optimize
> debug mode handling, but on the other hand, you really don't want to
> check if DEBUG is enabled 1 million times.
>
> If I recall though... a few years ago the answer to this question was
> that there's no reason why you can't use the C pre-processor to
> accomplish the same thing with PHP. The down side though is that then
> you lose debugging information such as the real line number on which an
> error occurs.
>
> Cheers,
> Rob.
>