Why are functions atomic?

John Nagle nagle at animats.com
Fri May 4 12:19:57 EDT 2007


Michael wrote:
> On May 2, 6:08 am, Carsten Haese <cars... at uniqsys.com> wrote:
> 
>>On Tue, 2007-05-01 at 22:21 -0700, Michael wrote:

> I agree the performance gains are minimal.  Using function defaults
> rather than closures, however, seemed much cleaner an more explicit to
> me.  For example, I have been bitten by the following before:
>
>>>>def f(x):
> 
> ...     def g():
> ...         x = x + 1

     Too cute.  Don't nest functions in Python; the scoping model
isn't really designed for it.

>>An overriding theme in this thread is that you are greatly concerned
>>with the speed of your solution rather than the structure and
>>readability of your code.
...
> 
> @define_options(first_option='abs_tol')
> def step(f,x,J,abs_tol=1e-12,rel_tol=1e-8,**kwargs):
>    """Take a step to minimize f(x) using the jacobian J.
>    Return (new_x,converged) where converged is true if the tolerance
>    has been met.
>    """

    Python probably isn't the right language for N-dimensional optimization
if performance is a major concern. That's a very compute-intensive operation. 
I've done it in C++, with heavy use of inlines, and had to work hard to
get the performance up.  (I was one of the first to do physics engines for
games and animation, which is a rather compute-intensive problem.)

    If you're doing number-crunching in Python, it's essential to use
NumPy or some other C library for matrix operations, or it's going to
take way too long.

					John Nagle



More information about the Python-list mailing list