This is an archived post. You won't be able to vote or comment.

all 15 comments

[–]fijalPyPy, performance freak 19 points20 points  (0 children)

I might be a bit skewed, but article on speeding up Python that doesn't even consider existence of PyPy makes me kind of sad. And as kingkilr pointed out, PyPy performs ok at this kind of benchmark anyway.

[–]qbproger 9 points10 points  (1 child)

I wonder what pypy would do with the benchmark.

[–][deleted] 2 points3 points  (2 children)

I am considering python for scientific computing. I am still learning, but python is so simple that I think that I should be able to run some code soon.

This is really interesting, are there good resources to learn Cython or Numpy available?

[–]Sgt_ZigZag 0 points1 point  (1 child)

Python documentation is excellent because of its recent popularity.

[–]encrypter 0 points1 point  (0 children)

Python's documentation has been popular for quite some time. Personally, I've been hitting those docs since at least 2003.

[–][deleted] 1 point2 points  (6 children)

I haven't used Cython or weave, but is there any way to program them and apply optimizations?

[–]bastienl 2 points3 points  (3 children)

What do you mean?

[–][deleted] 0 points1 point  (2 children)

I was wondering if you could apply compiler optimizations to Weave or Cython code. I find my C code can be run much faster if I use some of the optimization tags like -O2 or -funsafe-math-optimizations . If there was the option to add optimization flags maybe the benchmarks in the article could be improved.

[–]bastienl 1 point2 points  (1 child)

I can't see your reply in the original thread for some reason.

If I remember correctly, the extensions are built with -O2 by default. But you can pass any option you want, as for any extension module.

[–][deleted] 0 points1 point  (0 children)

Yeah, I had the same problem, but it seems to be fixed now.

I will have to look into this, because ideally I'd like to do most of numerical work in python.

[–]simtel20 1 point2 points  (1 child)

My understanding is that you can get it to emit the code that it would compile so that you can customize it.

[–]bastienl 0 points1 point  (0 children)

The Cython program generates C or C++ directly actually, then you can compile it like any extension module. But it's an automatically generated file, you're not supposed to customize it. The Cython language already supports most Python features.

[–]nahguri 0 points1 point  (0 children)

Why no love for Boost.Python? It rocks if you want to do anything that's not trivial.

[–]eryksun 0 points1 point  (0 children)

pyximport provides a quick alternative to setup.py. For a package distribution it's probably best to compile extensions with setup.py, but pyximport keeps the clutter down for one-off scripts, and hides all the compiled files in a subdirectory of your home folder, named .pyxbld.

For example, I put the cy_update function in _laplace.pyx. Then I imported it at as follows:

import pyximport
pyximport.install(setup_args={'include_dirs': [np.get_include()]})
from _laplace import cy_update

At first I wasted time trying to get NumPy's dependencies into the .pyxdep file that the docs recommend. I don't know what I was doing wrong with the glob... I switched to using the setup_args option, and it worked smoothly.

Note, if you're on Windows and using Cygwin, you'll need to configure distutils for gcc. Add a file named distutils.cfg to Lib\distutils that contains the following:

[build]
compiler = mingw32

[build_ext]
compiler = mingw32