Directly calling threaded class instance methods and attributes

Peter Hansen peter at engcorp.com
Fri Oct 29 15:20:27 CEST 2004


Josiah Carlson wrote:
>>>>def goo(n):
> 
> ...     global val2
> ...     _lock = lock
> ...     for i in xrange(n):
> ...         _lock.acquire()
> ...         val2 += 1
> ...         _lock.release()
> 
> 
> ...for a little bit faster (though the locking/unlocking will overwhelm
> the actual time spent.

If you're intent on making the code less maintainable in
order to achieve tiny improvements in speed, at least
store local references to the entire method, not just
to the object:

     global val2
     acq = lock.acquire
     rel = lock.release
     for i in xrange(n):
         acq()
         val2 += 1
         rel()

-Peter



More information about the Python-list mailing list