Python vs. Perl, which is better to learn?

Patrick W quitelikely at yahoo.com.au
Thu May 9 02:04:11 EDT 2002


"James J. Besemer" <jb at cascade-sys.com> writes:

> In other words are you seriously saying there are not "many"
> applications in the world where Python probably would be too slow?

IOW, how many is "many" and how fast is "fast enough"? These questions
aren't really answerable without knowing more than we can know. So
let's bury the dead and subsequently flogged horse, eh? ;-)

On a slightly different note, I've found that Python has occasionally
helped me to *speed up* an application quite significantly. Reason
being that, in C++, poor algorithms were not as easily noticeable
whereas in Python they're intolerable.

As a simple (and contrived) example, take the following fibonacci
functions in C++.

int fib1(int n)
{
    // numbskull version
    if (n < 3) return 1;
    return fib(n-2) + fib(n-1);
}

int fib2(int n, int a=1, int b=1)
{
    // tail recursive version
    if (n < 3) return b;
    return fib2(n-1, b, a+b);
}

In C++, the speed difference isn't *very* dramatic, but running the
equivalents in Python reveals the horrible inefficiency of the first.

def fib1(n):
    if n < 3: return 1
    return fib(n-2) + fib(n-1)

def fib2(n, a=1, b=1):
    if n < 3: return b
    return fib2(n-1, b, a+b)

(Yeah, I know: iterative version would be better still).

Actually, here's a more realistic example: The other day somebody
asked for advice on how to store and retrieve entries in a dictionary
using an arbitrary combination of elements in a tuple as keys. Not
understanding his problem correctly, I suggested that he use a
permutation algorithm as part of the solution. If he'd taken my
advice, no language in the world could have saved his skin ;-)

The best way to produce fast code is to understand the problem. By
minimising the irrelevant stuff, Python certainly helps with that. It
leaves more time and headspace for re-thinking the 'whats' as well as
the 'hows'.

Cheers,
P.



More information about the Python-list mailing list