Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Using Python for programming algorithms (http://www.velocityreviews.com/forums/t615422-using-python-for-programming-algorithms.html)

Vicent Giner 05-17-2008 10:32 PM

Using Python for programming algorithms
 
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.

Colin J. Williams 05-18-2008 12:00 AM

Re: Using Python for programming algorithms
 
Vicent Giner wrote:
> 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.

castironpi 05-18-2008 12:05 AM

Re: Using Python for programming algorithms
 
On May 17, 5:32*pm, Vicent Giner <vgi...@gmail.com> wrote:
> 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.

Vicent Giner 05-18-2008 12:53 AM

Re: Using Python for programming algorithms
 

>
> 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!

--
Vicent

sturlamolden 05-18-2008 01:44 AM

Re: Using Python for programming algorithms
 
On May 18, 12:32 am, Vicent Giner <vgi...@gmail.com> wrote:

> * 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.





> Thank you in advance.



Henrique Dante de Almeida 05-18-2008 03:25 AM

Re: Using Python for programming algorithms
 
On May 17, 7:32*pm, Vicent Giner <vgi...@gmail.com> wrote:
> 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.

inhahe 05-18-2008 03:46 AM

Re: Using Python for programming algorithms
 
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. ;)






sturlamolden 05-18-2008 12:20 PM

Re: Using Python for programming algorithms
 
On May 18, 5:46 am, "inhahe" <inh...@gmail.com> wrote:

> 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.

















sturlamolden 05-18-2008 07:50 PM

Re: Using Python for programming algorithms
 
On May 18, 4:20 pm, David C. Ullrich <dullr...@sprynet.com> wrote:

> 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.












Jaap Spies 05-18-2008 08:29 PM

Re: Using Python for programming algorithms
 
Vicent Giner wrote:
> 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


All times are GMT. The time now is 02:41 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.