Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
conditional running of code portion
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
JW Huang  
View profile  
 More options Aug 5 2012, 12:16 am
Newsgroups: comp.lang.python
From: JW Huang <ngc...@gmail.com>
Date: Sat, 4 Aug 2012 21:16:04 -0700 (PDT)
Local: Sun, Aug 5 2012 12:16 am
Subject: conditional running of code portion
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Aug 5 2012, 2:30 am
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 05 Aug 2012 06:30:49 GMT
Local: Sun, Aug 5 2012 2:30 am
Subject: Re: conditional running of code portion

On Sat, 04 Aug 2012 21:16:04 -0700, JW Huang wrote:
> 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)

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Serhiy Storchaka  
View profile  
 More options Aug 5 2012, 12:40 pm
Newsgroups: comp.lang.python
From: Serhiy Storchaka <storch...@gmail.com>
Date: Sun, 05 Aug 2012 19:40:40 +0300
Local: Sun, Aug 5 2012 12:40 pm
Subject: Re: conditional running of code portion
On 05.08.12 09:30, Steven D'Aprano wrote:

> 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)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven W. Orr  
View profile  
 More options Aug 5 2012, 10:50 pm
Newsgroups: comp.lang.python
From: "Steven W. Orr" <ste...@syslang.net>
Date: Sun, 05 Aug 2012 22:50:02 -0400
Local: Sun, Aug 5 2012 10:50 pm
Subject: Re: conditional running of code portion
On 8/5/2012 12:43 AM, Ramchandra Apte wrote:

> 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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dieter Maurer  
View profile  
 More options Aug 6 2012, 1:02 pm
Newsgroups: comp.lang.python
From: Dieter Maurer <die...@handshake.de>
Date: Mon, 06 Aug 2012 19:02:24 +0200
Local: Mon, Aug 6 2012 1:02 pm
Subject: Re: conditional running of code portion

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Serhiy Storchaka  
View profile  
 More options Aug 6 2012, 4:59 pm
Newsgroups: comp.lang.python
From: Serhiy Storchaka <storch...@gmail.com>
Date: Mon, 06 Aug 2012 23:59:59 +0300
Local: Mon, Aug 6 2012 4:59 pm
Subject: Re: conditional running of code portion
On 06.08.12 20:02, Dieter Maurer wrote:

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)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »