473,757 Members | 5,404 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.
Jun 27 '08
53 5251
On May 19, 6:52*am, Bruno Desthuilliers <bruno.
42.desthuilli.. .@websiteburo.i nvalidwrote:
Henrique Dante de Almeida a écrit :
On May 17, 7:32 pm, Vicent Giner <vgi...@gmail.c omwrote:
Hello.

(snip)
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.

(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.
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
Jun 27 '08 #21
On May 19, 6:11 pm, Henrique Dante de Almeida <hda...@gmail.c om>
wrote:
On May 19, 7:03 am, Bruno Desthuilliers <bruno.
I'm pretty sure about that: when the algorithms take 4 hours to test
a single execution, you value processor time.

Yes, of course, but that should mean that I have to do it better, in
the programming step (I would have to re-program or re-implement my
algorithm). And I think the problem would be the same in any other
language, wouldn't it?

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.
Are there such toolkits in other languages? I am not sure they exist
in C, for example.

By the way, is it possible (and easy) to call a C function from a
Python program??

Jun 27 '08 #22
In article <hi************ ******@newsfe14 .ams2>,
Roel Schroeven <rs************ ****@fastmail.f mwrote:
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.

But how about this C/C++ interpreter. Dr. Dobbs article:
http://www.ddj.com/cpp/184402054. Title and first two paragraphs:

Ch: A C/C++ Interpreter for Script Computing
Interactive computing in C

Ch is a complete C interpreter that supports all language features and
standard libraries of the ISO C90 Standard, but extends C with many
high-level features such as string type and computational arrays as
first-class objects.

For some tasks, C and its compile/ link/execute/debug process are not
productive. As computer hardware becomes cheaper and faster, to be
productive and cost effective, script computing in C/C++ can be an
appealing solution. To this end, we have developed Ch, an embeddable
C/C++ interpreter for cross-platform scripting, shell programming, 2D/3D
plotting, numerical computing, and embedded scripting [1].

--
-- Lou Pecora
Jun 27 '08 #23
On 19 mai, 15:30, Roel Schroeven <rschroev_nospa m...@fastmail.f m>
wrote:
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.
"In theory" ??? Heck, both points above are mere facts. Well, I may
accept that the 2nd one is a bit overgeneralized , since IIRC there's
an experimental Python to javascript "compiler" in Pypy, but...
But I'm still inclined to disagree with it, since the practical reality is different.
Do you mean that how source code written in a language (that is : a
grammar + a syntax) finally become a set of instructions executed by
a CPU depends on the language (I repeat : a grammer + a syntax), and
not on a piece of software turning the source code into something that
can actually be executed by the CPU ? Or that there exists a (working
and usable) implementation of the Python language that does not use an
intermediate byte-code compilation ? If the latest, I'd happly
recognize my error if proven wrong. But on the first point, I'm afraid
that, well, a fact is a fact is a fact.
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,
Obviously, yes - at least for all assembly language I've seen so far.
But whoever said otherwise ?
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.
Nope. The facts are that
1/ Python (the language) has *not* been designed with ease of
implementation of an optimizing native-code compiler in mind, and
2/ CPython (the first and reference implementation) has been designed
to use a byte-code + VM scheme
C OTOH was designed to be compiled to assembly code (or directly to
machine code)
Note quite. C has been designed to make it as easy as possible to
write either a C to assembly or C to native binary code compiler.
and as a result there are no (or virtually) no
implementations that interpret C or compile it to bytecode.
There's at least one (possibly incomplete) C interpreter. FWIW, it
would not be harder (and possibly simpler) to write a byte-code+VM
based C implementation than it is to write CPython, Jython or
IronPython. The point is that it's just useless - C is a (very) low-
level language, and the only reason to use C is that you'll find a
pretty good optimizing native-code compiler on almost any platform -
sometimes even before the CPU physically exists.
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.
There's a very naive belief we saw every here and then here, which is
that "Python would be faster if it was compiled to native code". The
point is that, given Python's (as a language) extrem dynamism,
compiling it to native code wouldn't buy you much in terms of raw
performances. The problem is not with writing a native-code
compiler[1}, but with writing an *optimising* native-code compiler.
FWIW, even Java with it's brain-dead static type-system gained more
from JIT compilation in the VM than from being directly compiled to
native code.

[1] not that I would personnaly be able to do so in a reasonable
amount of time, but given how many talented programmers have been and
are still working on making as fast as possible Python implementions,
it seems obvious that such a thing would already exists if there was
any point working on it.
I also think that there's a large difference
between byte code and machine code
No ? Really ? Now *this* is a scoop, you know ? <g>
(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'm not trying to "trivialize " anything. I'm just getting fed up with
this "Python is an interpreted and therefore slow language" non-
sense. Python is a language, and as such is neither slow nor fast nor
interpreted nor compiled nor <insert any implementation related stuff
here>. And while CPython is not blazingly fast for computation-heavy
stuff, it's not because it is "interprete d" - which it is not for a
strict definition of "interprete d", but anyway... - but because
*optimizing* execution of an highly dynamic language is nothing,
well, err, trivial.

Jun 27 '08 #24
On 19 mai, 18:11, Henrique Dante de Almeida <hda...@gmail.c omwrote:
On May 19, 7:03 am, Bruno Desthuilliers <bruno.

42.desthuilli.. .@websiteburo.i nvalidwrote:
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 ?-)

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.
I'm no expert here, but this sounds like a sensible argument to me.

OTHO, if the OP ends up spending more time writing boilerplate code
and fighting with gory implementation details than working on the
algorithm themselves, he might find than relative difference between
two possible algorithms (like one takes 4 hours and the second take 2
1/2 hours, but each took less than one hour to implement) is much more
important than having the first one taking 4 minutes, the second 2 1/2
minutes, and each having take 15 days to write and debug...
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.
There's at least Psyco (if you're willing and able to restrict
yourself from using some of the most dynamic parts of Python - which
might not be a problem here). One could also mention stuff like Pyrex
and Cython.
Jun 27 '08 #25
On 19 mai, 20:07, Vicent Giner <vgi...@gmail.c omwrote:
On May 19, 6:11 pm, Henrique Dante de Almeida <hda...@gmail.c om>
wrote:
(snip)
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.

Are there such toolkits in other languages? I am not sure they exist
in C, for example.
Well... They do - they are called 'C compilers' !-) As Roel Schroven
mentioned - and he is at least partially right on this point - C has
been designed to make optimizing C compiler not to hairy to write.
By the way, is it possible (and easy) to call a C function from a
Python program??
Possible, yes, indeed. Easy depends on your definition of easiness,
but going down to C for computation-heavy performance-critical parts
of a program or library is not that uncommon.
Jun 27 '08 #26
On 19 mai, 17:53, Henrique Dante de Almeida <hda...@gmail.c omwrote:

(snip)
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
for beer in beers:
if beer.entered_he nrique_digestiv e_system_last_n ight:
beer.blame()

!-)
Jun 27 '08 #27
On May 19, 3:07*pm, Vicent Giner <vgi...@gmail.c omwrote:
>
Yes, of course, but that should mean that I have to do it better, in
the programming step (I would have to re-program or re-implement my
algorithm). And I think the problem would be the same in any other
language, wouldn't it?
The idea is that a C version of the same program could take, eg. 0,4
hours. But I think we have an authoritative answer here, see Robin
Becker's post (even though he programmed the problem, not the
algorithm). :-)
Are there such toolkits in other languages? I am not sure they exist
in C, for example.
I'm sure there are a lot of toolkits for linear programming (can't
tell about other solvers). glpk is the GNU implementation. It even has
its own built-in language (Mathprog). Someone posted an interesting
link of a python wrapper: "OpenOpt"

http://scipy.org/scipy/scikits/wiki/OpenOpt

It supports many solvers. It may be interesting for you, since there
are some non-linear and "global" problem solvers:

http://scipy.org/scipy/scikits/wiki/OOClasses
By the way, is it possible (and easy) to call a C function from a
Python program??
Yes. The easiest way I know of is using SWIG. People will recommend
you Cython too.
Jun 27 '08 #28
On May 19, 5:25*pm, "bruno.desthuil li...@gmail.com "
<bruno.desthuil li...@gmail.com wrote:
There's at least one (possibly incomplete) C interpreter. FWIW, it
would not be harder (and possibly simpler) to write a byte-code+VM
based C implementation than it is to write CPython, Jython or
You may (right now, readily, without experimental software) compile C
to, for example, llvm bytecode, interpret it in the VM, JIT-compile
it, native-compile it, etc. There's also experimental support for
compiling C to the JVM.

Notice that you usually want to optimize C code, so it will be harder
than writing a python interpreter.
IronPython. The point is that it's just useless - C is a (very) low-
It's not useless. Consider that you may distribute your C application
in byte-code instead of native code and during the installation
process, it is native compiled and optimized exactly to your
architecture (like a better "Gentoo", with the compilation split
between you and the user).
point is that, given Python's (as a language) extrem dynamism,
compiling it to native code wouldn't buy you much in terms of raw
performances. The problem is not with writing a native-code
compiler[1}, but with writing an *optimising* native-code compiler.
That's the job of pypy folks. And they are getting there. They use a
python subset, called RPython:

http://morepypy.blogspot.com/2008/01...er-than-c.html
(note: the test in the site compares garbage collection speed)

BTW, here's how to compile RPython to (optimized) native code:
http://codespeak.net/pypy/dist/pypy/...one-howto.html

And here is the document that talks about all that:
http://codespeak.net/pypy/dist/pypy/...anslation.html
I'm not trying to "trivialize " anything. I'm just getting fed up with
this "Python is an interpreted and therefore slow language" non-
sense. *Python is a language, and as such is neither slow nor fast nor
I'm sorry for creating all those offtopic posts. I feel so ashamed :-
(. Well, not really. :-P

My suggestion was to use code suitable for optimization (not
considering things that you'd need to worry about the way you write
code, like RPython, or psyco). That's all. :-)
Jun 27 '08 #29
On May 19, 5:35*pm, "bruno.desthuil li...@gmail.com "
<bruno.desthuil li...@gmail.com wrote:
*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.

There's at least Psyco (if you're willing and able to restrict
yourself from using some of the most dynamic parts of Python - which
might not be a problem here). *One could also mention stuff like Pyrex
and Cython.
I meant toolkits for "optimizati on problems", not "code
optimization".
Jun 27 '08 #30

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

33
3983
by: Joe Cheng | last post by:
I'm curious about something... many Artima.com members who have a Java background and learned Python have come to the conclusion that Java and Python are highly complimentary languages. They would never consider filling the place Java has in their toolbox with Python, but recognize there are many tasks where it really pays to add Python to the mix. I want to ask you hard-core c.l.p Pythonistas: Do you use Python for everything? (and...
226
12653
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
68
5882
by: Lad | last post by:
Is anyone capable of providing Python advantages over PHP if there are any? Cheers, L.
20
2160
by: xeys_00 | last post by:
I posted a article earlier pertaining programming for my boss. Now I am gonna ask a question about programming for myself. I just finished my first C++ Class. Next semester is a class on encryption(and it's probably gonna be a math class too). And finally back in programming in the fall with C++ and Java 1. The C++ will cover pointers, and linked lists, sorting algorithms, etc... I run linux and OS X. I have read in the old days that C was...
13
5905
by: abhinav | last post by:
Hi guys.I have to implement a topical crawler as a part of my project.What language should i implement C or Python?Python though has fast development cycle but my concern is speed also.I want to strke a balance between development speed and crawler speed.Since Python is an interpreted language it is rather slow.The crawler which will be working on huge set of pages should be as fast as possible.One possible implementation would be...
27
2058
by: hacker1017 | last post by:
im just asking out of curiosity.
2
3682
by: Xiao Jianfeng | last post by:
Hi all, I am looking for a genetic algorithms package for Python. I have googled the web before posting and found some links. The link of pygene(http://www.freenet.org.nz/python/pygene) cannot be opened. I also tried the recipe on ASPN, but it is too simple for my application, and the ga model in SciPy, which is in testing in the "sandbox".
11
3778
by: efrat | last post by:
Hello, I'm planning to use Python in order to teach a DSA (data structures and algorithms) course in an academic institute. If you could help out with the following questions, I'd sure appreciate it: 1. What exactly is a Python list? If one writes a, then is the complexity Theta(n)? If this is O(1), then why was the name "list" chosen? If this is indeed Theta(n), then what alternative should be used? (array does not seem suited for...
18
7436
by: Jens | last post by:
I'm starting a project in data mining, and I'm considering Python and Java as possible platforms. I'm conserned by performance. Most benchmarks report that Java is about 10-15 times faster than Python, and my own experiments confirms this. I could imagine this to become a problem for very large datasets. How good is the integration with MySQL in Python?
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.