Mombu the Php Forum sponsored links

Go Back   Mombu the Php Forum > Php > Conditional compilation
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 29th April 06:20
herman.gomez
External User
 
Posts: 1
Default Conditional compilation



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.
  Reply With Quote


  sponsored links


2 29th April 09:32
mrclay
External User
 
Posts: 1
Default Conditional compilation



If performance is that critical, design the debugging code so you can
strip it out in a build script. If you don't want to do that, this is
probably the fastest alternative:

define('MYAPP_DEBUG', true);
...
MYAPP_DEBUG && include('debugCode.php');
...

Steve
  Reply With Quote
3 29th April 09:32
nospam
External User
 
Posts: 1
Default Conditional compilation


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.

-Shawn
  Reply With Quote
4 29th April 09:32
robert
External User
 
Posts: 1
Default Conditional compilation


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.
--
http://www.interjinn.com
Application and Templating Framework for PHP
  Reply With Quote
5 29th April 09:33
nospam
External User
 
Posts: 1
Default Conditional compilation


Great! Then the answer is: wait, wait,

write it in C!

-Shawn
  Reply With Quote
6 29th April 09:33
robert
External User
 
Posts: 1
Default Conditional compilation


Well PHP does have a great extension system so you can plug in your own
C code

Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
  Reply With Quote
7 29th April 23:20
micah
External User
 
Posts: 1
Default Conditional compilation


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
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666