if not DEBUG: log = null_log

@(none) "simonb\" at (none)
Tue Jul 12 23:49:25 EDT 2005


Bengt Richter wrote:
> On Wed, 13 Jul 2005 11:00:14 +1000, Simon Burton <simon at arrowtheory.com> wrote:
> 
> 
>>Hi,
>>
>>I'm after a no-op command, so that i can redirect
>>logging commands in performance critical code.
>>
>>Something like this:
>>
>>def log(*args): print args
>>def null_log(*args): pass
>>if not DEBUG: log = null_log
>>
>>is unacceptable because of the overhead of calling
>>functions in python.
>>
> 
> I think you could make the existence of log calls dependent on
> whether you compile with an optimize flag by abusing
> an assert statement, e.g.,
> 
>     assert log(some, args) or True
> 

This is a session with the -O flag, so asserts disapear:

 >>> from time import time
 >>>
 >>> def count(N=10000000):
...   for i in xrange(N): i=0
...
 >>>
 >>> t=time(); count(); print time()-t

0.821492910385
 >>>
 >>> def count_call(N=10000000):
...   for i in xrange(N): foo()
...
 >>> t=time(); count_call(); print time()-t
3.50276303291
 >>>
 >>> def log(): print "log"
...
 >>>
 >>> def count_assert(N=10000000):
...   for i in xrange(N): assert log()
...
 >>>
 >>> t=time(); count_assert(); time()-t
0.61060500144958496

Woohoo!!


Simon.



More information about the Python-list mailing list