Using Python for programming algorithms

V

Vicent Giner

Hello.

I am new to Python. It seems a very interesting language to me. Its
simplicity is very attractive.

However, it is usually said that Python is not a compiled but
interpreted programming language —I mean, it is not like C, in that
sense.

I am working on my PhD Thesis, which is about Operations Research,
heuristic algorithms, etc., and I am considering the possibility of
programming all my algorithms in Python.

The usual alternative is C, but I like Python more.

The main drawbacks I see to using Python are these:

* As far as I understand, the fact that Python is not a compiled
language makes it slower than C, when performing huge amounts of
computations within an algorithm or program.

* I don't know how likely it is to find libraries in Python related to
my research field.

* I know Python is a "serious" and mature programming language, of
course. But I do not know if it is seen as "just funny" in a research
context. Is Python considered as a good programming language for
implementing Operations Research algorithms, such as heuristics and
other soft-computing algorithms?

Maybe this is not the right forum, but maybe you can give me some
hints or tips...

Thank you in advance.
 
C

Colin J. Williams

Vicent said:
Hello.

I am new to Python. It seems a very interesting language to me. Its
simplicity is very attractive.

However, it is usually said that Python is not a compiled but
interpreted programming language —I mean, it is not like C, in that
sense.

I am working on my PhD Thesis, which is about Operations Research,
heuristic algorithms, etc., and I am considering the possibility of
programming all my algorithms in Python.

The usual alternative is C, but I like Python more.

The main drawbacks I see to using Python are these:

* As far as I understand, the fact that Python is not a compiled
language makes it slower than C, when performing huge amounts of
computations within an algorithm or program.

The usual answer is that development
time is more important than running time.

Since you are likely to be using arrays,
you might look at numpy, where the
number crunching is using compiled C code.
* I don't know how likely it is to find libraries in Python related to
my research field.

* I know Python is a "serious" and mature programming language, of
course. But I do not know if it is seen as "just funny" in a research
context. Is Python considered as a good programming language for
implementing Operations Research algorithms, such as heuristics and
other soft-computing algorithms?

Try Google with Python and your area of
interest. You could well find
Python-based packages which meet your needs.
Maybe this is not the right forum, but maybe you can give me some
hints or tips...

Thank you in advance.

Good luck.

Colin W.
 
C

castironpi

Hello.

I am new to Python. It seems a very interesting language to me. Its
simplicity is very attractive.

However, it is usually said that Python is not a compiled but
interpreted programming language —I mean, it is not like C, in that
sense.

I am working on my PhD Thesis, which is about Operations Research,
heuristic algorithms, etc., and I am considering the possibility of
programming all my algorithms in Python.

The usual alternative is C, but I like Python more.

The main drawbacks I see to using Python are these:

* As far as I understand, the fact that Python is not a compiled
language makes it slower than C, when performing huge amounts of
computations within an algorithm or program.

* I don't know how likely it is to find libraries in Python related to
my research field.

* I know Python is a "serious" and mature programming language, of
course. But I do not know if it is seen as "just funny" in a research
context. Is Python considered as a good programming language for
implementing Operations Research algorithms, such as heuristics and
other soft-computing algorithms?

Maybe this is not the right forum, but maybe you can give me some
hints or tips...

Thank you in advance.

You're hearing from 'impossible and useless'-- neither operations.

'Stacks' are pretty important to Python (there is a Stackless Python,
I understand), which makes persistence a little more handy. It's
still a computer and still a language. You may be asking how well its
best speakers know, and look at that, I can't tell you. Some of the
fundamentals of Python may be unexplored to date, as its from the 90s,
and stacks are elements.

I, for one, will assume you're interested in control operations, which
yes Python has, and control is important. The standard library is a
good size to me (I wouldn't go doubling). There's a ready graphics
module. There are code-primitive literals, including lists -and- a
tuple. I think you're looking for the selling points of dynamic
assignment (a.barproperty= 'unheardof'), typefreeness (a= [2,'bcd']),
dynamic execution (exec('print 2'), which promotes a possibility of
self-referentiality), type-aware function pointers, variable-length
procedure arguments, and platform independence.

I think you just asked at the right time. Yes that's an impressive
list.

There is one catch to Python, of the importance of which of the powers
that be, I am unaware. But I do know what you are liable to find on
the newsgroup. Now, with thousands of dollars of institution time on
the money, what control? I will be tentatively assuming that you are
not covertly comparing other languages. I don't think you'll like it
if you're unwise.
 
V

Vicent Giner

The usual answer is that development
time is more important than running time.

OK, thank you for your answer.

I just wanted to check if it was a naive idea trying to use Python
instead of C in order to implement my own algorithms, and other
research-related code.

Since you are likely to be using arrays,
you might look at numpy, where the
number crunching is using compiled C code.

NumPy seeems to be a very interesting and powerful package. Thank you
again.

Try Google with Python and your area of
interest. You could well find
Python-based packages which meet your needs.

Yes, I've been giving a try, and I've found some interesting links.

Thank you!
 
S

sturlamolden

* As far as I understand, the fact that Python is not a compiled
language makes it slower than C, when performing huge amounts of
computations within an algorithm or program.

First of all: whatever you do, use NumPy for all numerical work (and
possibly Scipy). Remember that Python with NumPy tend to be faster
than Matlab. Anything that applies to Matlab regarding vectorization
for speed also applies to NumPy.

If your program runs too slowly, try to use the psyco jit compiler
first. If that doesn't help, try one of the following:

- use Cython or Pyrex and compile your (modified) Python code
- inline C++ using scipy.weave
- write a function in C and call it using ctypes
- write a function in Fortran and make it callable from Python using
f2py

Usually, only small bottlenecks matter when it comes to overall
performance. It is also notoriously difficult to guess where they are.
Therefore: write everything in Python first, then profile your code to
identify bottlenecks. Only important bottlenecks need to be translated
to Pyrex, C or Fortran.

http://www.scipy.org/PerformancePython

* I know Python is a "serious" and mature programming language, of
course. But I do not know if it is seen as "just funny" in a research
context.

Google NumPy, SciPy, Matplolib and Sage.

NASA uses Python to process image data from the Hubble telescope.
 
H

Henrique Dante de Almeida

Hello.

I am new to Python. It seems a very interesting language to me. Its
simplicity is very attractive.

However, it is usually said that Python is not a compiled but
interpreted programming language —I mean, it is not like C, in that
sense.

I am working on my PhD Thesis, which is about Operations Research,
heuristic algorithms, etc., and I am considering the possibility of
programming all my algorithms in Python.

The usual alternative is C, but I like Python more.

The main drawbacks I see to using Python are these:

* As far as I understand, the fact that Python is not a compiled
language makes it slower than C, when performing huge amounts of
computations within an algorithm or program.

* I don't know how likely it is to find libraries in Python related to
my research field.

* I know Python is a "serious" and mature programming language, of
course. But I do not know if it is seen as "just funny" in a research
context. Is Python considered as a good programming language for
implementing Operations Research algorithms, such as heuristics and
other soft-computing algorithms?

Maybe this is not the right forum, but maybe you can give me some
hints or tips...

Thank you in advance.

I guess that python is not a good language for that. Algorithms
implemented in plain python are many times slower than C ones
(hundreds ?). In practice, algorithms are written in C and wrapped in
python. I have near zero experience in operations research, but once I
looked for linear programming toolkits for python and IIRC, I only
could find a trivial wrapper for glpk (called pulp).

My opinion: choose compiled or byte compiled languages. Choose the
language paradigm that best suit the algorithms.
 
I

inhahe

what little I know:

The numbers I heard are that Python is 10-100 times slower than C. So use
Python if you can wait 10-100 times longer. Although it won't really be
that slow using numpy and/or psyco.

Python seems to have a really extensive reportoire of modules available for
it. Although I don't know about things in the math field. If what you want
is obscure enough, then you might find it for C/C++ and not Python. You
might find out in a few seconds by googling.

The advantage to Python (other than production time), is that it would be a
lot simpler and more readable simply as a syntax for conveying algorithms.
It would be like pseudo-code... but runnable. ;)
 
S

sturlamolden

The numbers I heard are that Python is 10-100 times slower than C.

Only true if you use Python as if it was a dialect of Visual Basic. If
you use the right tool, like NumPy, Python can be fast enough. Also
note that Python is not slower than any other language (including C)
if the code is i/o bound. As it turns out, most code is i/o bound,
even many scientific programs.

In scientific research, CPU time is cheap and time spent programming
is expensive. Instead of optimizing code that runs too slowly, it is
often less expensive to use fancier hardware, like parallell
computers. For Python, we e.g. have mpi4py which gives us access to
MPI. It can be a good advice to write scientific software
parallelizable from the start.

I learned Pascal my first year in college. When I started programming
Matlab, I brought with me every habits of a novice Pascal programmer.
Needless to say, my programs ran excruciatingly slow. I learned C just
to write faster "mex" extensions for Matlab. But eventually, my skills
improved and I found that my Matlab programs did not need C anymore.
It took me almost 3 years to unlearn the bad habits I had acquired
while programming Pascal. It is very easy to blame the language, when
in fact it is the programmer who is not using it properly.
 
S

sturlamolden

Are you going to be doing research _about_ the
algorithms in question or is it going to be research
_using_ these algorithms to draw conclusions
about other things?

Most of the replies seem to be assuming the latter.
If it's the former then Python seems like definitely
an excellent choice - when you have want to try
something new it will be much faster trying it
out in Python,

I second this. Hence my previous statement that "In scientific
research, CPU time is cheap and time spent programming is expensive."
If it was not clear what I meant, your post can serve as a
clarification. But whether Giner is 'developing' or 'using'
algorithms, he should value his own labour more than the CPU's. CPU
labour (i.e. computation) is very cheap. Manual labour (i.e.
programming) is very expensive. He may in any case benefit from using
Python. Today, the preferred computer language amount scientists is
not Fortran77, but various high-level languages like Matlab, S, IDL,
Perl and Python.

A related question is: How much 'speed' is really needed? If Giner is
analyzing datasets using conventional statistics (ANOVA, multiple
regression, etc.), when will Python (with NumPy) cease to be
sufficient? In my experience, conventional statistics on a dataset of
100,000 or 1,000,000 samples can be regarded child's play on a modern
desktop computer. One really need HUGE amounts of data before it's
worthwhile to use anything else. If one can save a couple of seconds
CPU time by spending several hours programming, then the effort is not
just futile, it's downright wasteful and silly.


Something else that should be mentioned:

The complexity of the algorithm (the big-O notation) is much more
important for runtime performance than the choice of language. If you
can replace a O(N*N) with O(N log N), O(N) or O(1) it is always
adviceable to do so. An O(N*N) algorithm implemented in C is never
preferred over an O(N) algorithm written in Python. The only time when
C is preferred over Python is when N is large, but this is also when
O(N*N) is most painful. Pay attention to the algorithm is things are
running unbearably slow.

Python has highly tuned datatypes like lists, dicts and sets, which a
C programmer will have a hard time duplicating. This also applies to
built-in algorithms like 'timsort'. qsort in the C standard library or
anything a C programmer can whip up within a reasonable amount of time
simply doesn't compare. C vs. Python benchmarks that doesn't take this
into account will falsely put Python in a bad light.
 
J

Jaap Spies

Vicent said:
Hello.

I am new to Python. It seems a very interesting language to me. Its
simplicity is very attractive.

However, it is usually said that Python is not a compiled but
interpreted programming language —I mean, it is not like C, in that
sense.

I am working on my PhD Thesis, which is about Operations Research,
heuristic algorithms, etc., and I am considering the possibility of
programming all my algorithms in Python.

The usual alternative is C, but I like Python more.

The main drawbacks I see to using Python are these:

* As far as I understand, the fact that Python is not a compiled
language makes it slower than C, when performing huge amounts of
computations within an algorithm or program.

* I don't know how likely it is to find libraries in Python related to
my research field.

* I know Python is a "serious" and mature programming language, of
course. But I do not know if it is seen as "just funny" in a research
context. Is Python considered as a good programming language for
implementing Operations Research algorithms, such as heuristics and
other soft-computing algorithms?

You definitely should take a look at Sage: http://www.sagemath.org/

This may offer all you need, based on Python integrating a lot of
other programs!

Jaap
 
B

Bruno Desthuilliers

Henrique Dante de Almeida a écrit :
(snip)
I guess that python is not a good language for that. (snip)
My opinion: choose compiled or byte compiled languages.

Slightly OT (ie : not talking about computation-heavy alorgithm being
better implemented in C then wrapped in Python - this seems quite
obvious) but just a couple facts:

1/ being interpreted or compiled (for whatever definition of these
terms) is not a property of a language, but a property of an
implementation of a language.

2/ actually, all known Python implementations compile to byte-code.
 
B

Bruno Desthuilliers

Vicent Giner a écrit :
Hello.

I am new to Python. It seems a very interesting language to me. Its
simplicity is very attractive.

However, it is usually said that Python is not a compiled but
interpreted programming language

cf my answer to you and Henrique on this.

I am working on my PhD Thesis, which is about Operations Research,
heuristic algorithms, etc., and I am considering the possibility of
programming all my algorithms in Python.

The usual alternative is C, but I like Python more.

Then use it.
The main drawbacks I see to using Python are these:

* As far as I understand, the fact that Python is not a compiled
language makes it slower than C, when performing huge amounts of
computations within an algorithm or program.

In which way is this a problem here ? I thought your thesis was about
algorithm, not about implementation optimisation ? And if it's the
later, then even C might sometimes be too high level - you should drop
to assembly language.
* I don't know how likely it is to find libraries in Python related to
my research field.

I can't tell but you'd be surprised by the quantity of available Python
libs.
* I know Python is a "serious" and mature programming language, of
course. But I do not know if it is seen as "just funny" in a research
context. Is Python considered as a good programming language for
implementing Operations Research algorithms, such as heuristics and
other soft-computing algorithms?

Don't know if this answers your question, but it seems that at least
some authors consider it a good choice:
http://www.oreilly.com/catalog/9780596529321/

All code examples in this books are in Python - very badly written
Python, alas...
Maybe this is not the right forum, but maybe you can give me some
hints or tips...

Hem... Obviously, most people here will have a little biased, you know ?-)
 
T

Tim Golden

Bruno said:
2/ actually, all known Python implementations compile to byte-code.

In curiosity, did your "actually" mean, in the French sense, "at the moment"
or, in the English sense, "in contrast to something stated earlier"? Or maybe both?

TJG
 
B

Bruno Desthuilliers

Tim Golden a écrit :
In curiosity, did your "actually" mean, in the French sense, "at the
moment" or, in the English sense, "in contrast to something stated
earlier"? Or maybe both?

I mainly intented it to be understood in the English sense - but taking
it the French way would be ok too.
 
R

Roel Schroeven

Bruno Desthuilliers schreef:
1/ being interpreted or compiled (for whatever definition of these
terms) is not a property of a language, but a property of an
implementation of a language.

2/ actually, all known Python implementations compile to byte-code.

You keep saying that, and in theory you're right. But I'm still inclined
to disagree with it, since the practical reality is different. Python is
indeed compiled to byte code, but if you compare that byte code with
assembly code you'll see that there's a whole world of difference
between the two, largely because of the dynamical nature of Python. Fact
is that Python was designed from the start to run on a virtual machine,
not on the native hardware.

C OTOH was designed to be compiled to assembly code (or directly to
machine code) and as a result there are no (or virtually) no
implementations that interpret C or compile it to bytecode.

I love Python, but IMHO it's a bit silly to maintain that the fact that
Python compiles to byte code instead of assembly code/machine code is
purely a matter of implementation; on the contrary, I believe it's a
result of its design. I also think that there's a large difference
between byte code and machine code (in Python's case; I haven't looked
at other languages), and that it's a bit silly to try to trivialize that
difference.

--
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
-- Isaac Asimov

Roel Schroeven
 
D

Diez B. Roggisch

Roel said:
Bruno Desthuilliers schreef:

You keep saying that, and in theory you're right. But I'm still inclined
to disagree with it, since the practical reality is different. Python is
indeed compiled to byte code, but if you compare that byte code with
assembly code you'll see that there's a whole world of difference
between the two, largely because of the dynamical nature of Python. Fact
is that Python was designed from the start to run on a virtual machine,
not on the native hardware.

C OTOH was designed to be compiled to assembly code (or directly to
machine code) and as a result there are no (or virtually) no
implementations that interpret C or compile it to bytecode.

I love Python, but IMHO it's a bit silly to maintain that the fact that
Python compiles to byte code instead of assembly code/machine code is
purely a matter of implementation; on the contrary, I believe it's a
result of its design. I also think that there's a large difference
between byte code and machine code (in Python's case; I haven't looked
at other languages), and that it's a bit silly to try to trivialize that
difference.

I strongly disagree. See the Java which is compiled to bytecode
*explicitly*, jet the JVM-bytecode is closer to Python than to what C++ is
compiled to. But then, it's possible to have JIT-compilers that bring
machine code back into the picture.

And see python + psyco as JIT.

And OTOH see Objective-C and it's runtime for an example of a
machine-code-compiled language that still has huge dynamic runtime parts.

All these blur the image extremely and make Bruno's point: compilation is an
artifact, not some inherent property of a language.

Diez
 
H

Henrique Dante de Almeida

Henrique Dante de Almeida a écrit :


Slightly OT (ie : not talking about computation-heavy alorgithm being
better implemented in C then wrapped in Python - this seems quite
obvious) but just a couple facts:

1/ being interpreted or compiled (for whatever definition of these
terms) is not a property of a language, but a property of an
implementation of a language.

2/ actually, all known Python implementations compile to byte-code.

Yes, I was actually referring to statically typed JIT-compiled
languages. Sorry about that, blame the beers that entered my digestive
system that night. :p
 
H

Henrique Dante de Almeida

Vicent Giner a écrit :




cf my answer to you and Henrique on this.



Then use it.



In which way is this a problem here ? I thought your thesis was about
algorithm, not about implementation optimisation ? And if it's the
later, then even C might sometimes be too high level - you should drop
to assembly language.


I can't tell but you'd be surprised by the quantity of available Python
libs.


Don't know if this answers your question, but it seems that at least
some authors consider it a good choice:http://www.oreilly.com/catalog/9780596529321/

All code examples in this books are in Python - very badly written
Python, alas...


Hem... Obviously, most people here will have a little biased, you know ?-)

I agree with what most people here said, that the language doesn't
really matter, etc., but that simply does not apply to the specific
case of optimization research.

The little I know about optimization, even trivial problems may be
hairy problems. Naïve implementations simply don't finish and the
performance bottlenecks are not necessarily in the numeric computation
algorithms (so numpy doesn't help much here). If the guy is doing
research on that, it possible that he will work with thousands (or
millions) of weird constraints.

I'm pretty sure about that: when the algorithms take 4 hours to test
a single execution, you value processor time.

The situation would be simpler if there were good well-known toolkits
for optimization in python (like numpy for matrix operations), but
that's not the case.
 
R

Robin Becker

Vicent said:
Hello.

I am new to Python. It seems a very interesting language to me. Its
simplicity is very attractive.

However, it is usually said that Python is not a compiled but
interpreted programming language —I mean, it is not like C, in that
sense. ........
I programmed several large optimization problems in python. Mostly I used the
old numeric extension for the optimization parts eg vector calculations matrix
inversions etc etc, but I think numpy is used now. Since most of the work was
performed in the C extension we didn't really notice the 'slowness' of python's
interpreter.
 
V

Vicent Giner

Thank you very much for all the answers I've got.

As far as I have understood, Python can be a good alternative, or, at
least, a reasonable choice.

I intend to design new algorithms for a kind of Optimization problems,
and then I have to implement them and show/prove that they are good
enough, in terms of effectiveness (quality of the solution that the
algorithm is able to find, for some given "difficult" problems), and
not mainly in terms of efficiency (time to find the solution).

I mean, in order to prove that my algorithms are "good", I have to
compare them with the results given by other algorithms, in terms of
how much better is the solution given by my proposal, in front of the
previous ones. Only comparatives in terms of "number of iterations",
and not "time to find the solution", can be done (I think).

And I also realize, from your posted answers, that the point is doing
a good programming work, and that I will have to look very carefully
at all those packages and resources you have mentioned, to do a good
work at Python.

Any other suggestion will be welcomed. :)

Thank you very much again!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top