[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [igraph] speeding up the code
From: |
Tamas Nepusz |
Subject: |
Re: [igraph] speeding up the code |
Date: |
Thu, 11 Jun 2009 12:54:40 +0100 |
User-agent: |
Mutt/1.5.17 (2007-11-01) |
Hi Marco,
> I have just finished writing some fair complex code in python+igraph
> and I am now trying to speedup the code.
> Since I have never done something like this, I am looking for ideas.
Well, considering that igraph is written in C, the more time you are
able to spend "inside" igraph function calls, the faster your code will
be ;) Besides that, I guess it's better to optimize the Python code than
to rewrite the whole thing in C. Optimizing Python code is tricky, since
sometimes it goes against common sense if you are not familiar with
Python's internals. Read these essays as a start:
http://www.python.org/doc/essays/list2str.html
http://www.skymind.com/~ocrow/python_string/
Also, try to use list comprehensions instead of for loops whenever you
can. I.e., instead of doing the following:
result = []
for v in g.vs:
calculated_value = do_something(v)
result.append(calculated_value)
do this:
result = [do_something(v) for v in g.vs]
as this executes the for loop inside Python's C layer, so it's way
faster.
There's also a Python extension called Cython, which is sort of a
Python-to-C translator with some restrictions, so you can write
Python-like code which translates to C in the end. I have never used
igraph with Cython (I don't even know whether Cython can make use of
Python modules implemented in C or not), so if you try it, feel free to
share your experiences.
--
Tamas
pgpynvHeiiaED.pgp
Description: PGP signature