conditional running of code portion

J

JW Huang

Hi,

How can I implement something like C++'s conditional compile.

if VERBOSE_MODE: print debug information
else: do nothing

But I don't want this condition to be checked during runtime as it
will slow down the code.

Thanks in advance.
 
S

Steven D'Aprano

Hi,

How can I implement something like C++'s conditional compile.

if VERBOSE_MODE: print debug information else: do nothing

But I don't want this condition to be checked during runtime as it will
slow down the code.


You've profiled your code and found that checking a flag is a bottleneck
in your application? I'd hate to think you were wasting your time trying
to avoid "slowing down" your code by an insignificant amount that nobody
will ever notice.

In general, the way to do C-like conditional compiles is to use C, or at
least to use another language that isn't Python. Almost everything
happens at runtime in Python. Possibly PyPy can optimise away unnecessary
checks, but CPython doesn't.

One of the very few exceptions: you can disable code at compile time like
this:

if __debug__:
do_something()


compiles to the byte-code equivalent of:

do_something()


under normal circumstances, and to nothing at all if Python is running
with the -O optimize flag.


If you are working in a tight loop, you can do this:

if VERBOSE_FLAG:
for item in loop:
print(DEBUG_INFORMATION)
do_actual_work(item)
else:
for item in loop:
do_actual_work(item)
 
S

Serhiy Storchaka

If you are working in a tight loop, you can do this:

if VERBOSE_FLAG:
for item in loop:
print(DEBUG_INFORMATION)
do_actual_work(item)
else:
for item in loop:
do_actual_work(item)

Or this:

if VERBOSE_FLAG:
def do_work(item):
print(DEBUG_INFORMATION)
do_actual_work(item)
else:
do_work = do_actual_work

for item in loop:
do_work(item)
 
S

Steven W. Orr

Try pypreprocessor <http://code.google.com/p/pypreprocessor/> .
Better idea:
You should be using the logging <http://docs.python.org/library/logging.html>
module if you want to print debug information quickly.It uses threads and is
optimized to run fast.

I never saw pypreprocessor. Looks interesting. I have experience with Ned's cog
preprocessor.

http://nedbatchelder.com/code/cog/

I used this in something that was operating at the packet socket level. I had no
time to test if debug was true.



--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
 
D

Dieter Maurer

Serhiy Storchaka said:
Or this:

if VERBOSE_FLAG:
def do_work(item):
print(DEBUG_INFORMATION)
do_actual_work(item)
else:
do_work = do_actual_work

for item in loop:
do_work(item)

Be warned: a function call is *much* more expensive than an
"if variable:".
 
S

Serhiy Storchaka

Be warned: a function call is *much* more expensive than an
"if variable:".

As any actual work. As iteration.

Yet one way:

def verbose_iter(it):
for i in it:
print(DEBUG_INFORMATION)
yield i
....

if VERBOSE_FLAG:
loop = verbose_iter(loop)
for item in loop:
do_work(item)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top