Python is not bad ;-)

classic Classic list List threaded Threaded
37 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Cecil Westerhof
I am coding with Python again. I like it that the code is concise and
clear. But also that the performance is not bad.

I wrote Lucky Numbers in Clojure and Python.
When calling with 1E7 Clojure takes 12 seconds and Python 8 seconds.
When calling it with 1E8 Clojure takes all 4/8 cores and gets an out
of memory.
Python takes one core and is finished after 888 seconds.

When I call the Python version with 1E9 it is killed after 27 seconds.
I get a message Killed and the error code is 137. Probably also an out
of memory.

When I do that the computer is freezed a few times. That is a little
less nice. Does not happen with Clojure when it gets an out of memory.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Ben Finney-10
Cecil Westerhof <Cecil at decebal.nl> writes:

> I am coding with Python again.

Great to know!

> I like it that the code is concise and clear. But also that the
> performance is not bad.

The former is a property of Python, which is a programming language. I
agree with your assessment :-)

The latter is not a property of Python; a programming language doesn't
have runtime performance. Rather, runtime performance is a property of
some specific *implementation* ? that is, the runtime Python machine.

There are numerous Python runtimes, and they have different performance
characteristics on different hardware and operating systems.

    <URL:https://wiki.python.org/moin/PythonImplementations>

You might be talking about performance on CPython. But you might not! I
don't know.

Have you looked at PyPy ? Python implemented in Python ? and compared
its performance?

--
 \     ?I went camping and borrowed a circus tent by mistake. I didn't |
  `\      notice until I got it set up. People complained because they |
_o__)                           couldn't see the lake.? ?Steven Wright |
Ben Finney



Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Marko Rauhamaa
In reply to this post by Cecil Westerhof
Ben Finney <ben+python at benfinney.id.au>:

> The latter is not a property of Python; a programming language doesn't
> have runtime performance. Rather, runtime performance is a property of
> some specific *implementation* ? that is, the runtime Python machine.
>
> There are numerous Python runtimes, and they have different
> performance characteristics on different hardware and operating
> systems.

Still, Python has features that defy effective optimization. Most
notably, Python's dot notation translates into a hash table lookup -- or
worse.

I currently carry three clubs in my professional golf bag: bash, python
and C. Java is a great programming language, but Python and C manage
everything Java would be useful for.


Marko


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Chris Angelico
On Thu, Apr 30, 2015 at 8:16 PM, Marko Rauhamaa <marko at pacujo.net> wrote:

> Ben Finney <ben+python at benfinney.id.au>:
>
>> The latter is not a property of Python; a programming language doesn't
>> have runtime performance. Rather, runtime performance is a property of
>> some specific *implementation* ? that is, the runtime Python machine.
>>
>> There are numerous Python runtimes, and they have different
>> performance characteristics on different hardware and operating
>> systems.
>
> Still, Python has features that defy effective optimization. Most
> notably, Python's dot notation translates into a hash table lookup -- or
> worse.
>
> I currently carry three clubs in my professional golf bag: bash, python
> and C. Java is a great programming language, but Python and C manage
> everything Java would be useful for.

(I carry a lot more clubs in my bag. The Ace of Clubs for me is Pike,
but Python comes in a close second; both are decently high
performance, quick to write code in, and come with extensive standard
libraries. Any bash script that grows to more than a page or so of
code usually finds itself rewritten in Python or Pike; C is mainly for
writing high level programming languages in.)

Most of the programs that I write spend their time on work far more
serious than looking up names in dictionaries. For instance, one of my
programs [1] shells out to avconv and sox to do a bunch of big file
conversions, doing its best to fill up my hard disk (eighty-odd gig of
intermediate files is a good start), and ultimately producing one
hefty video file. Another that I contribute heavily to [2] uses lame
to manipulate a bunch of .MP3 files and, ultimately, stream them down
an internet connection. A third [3] sleeps its entire life away,
either making network requests and waiting for the responses, or
outright sleep()ing until it needs to go do something again. If the
cost of run-time lookups of dotted names were to increase by an order
of magnitude, not one of them would materially change in performance.
Sure, you can do microbenchmarks that show that Python takes X times
longer to parse "x.y.z" than Java does, but if that's seriously
impacting your real-world code, what are you doing?

About the only time when Python performance makes a real difference is
on startup. Mercurial, for instance, has to be invoked, initialized,
and shut down, for every command. (That's why git tends to outdo it in
a lot of ways, thanks to being written mainly in C and Perl.) So yes,
there are efforts every now and then to cut the startup time, where
however-many modules all have to get imported and set up. In the most
micro of microbenchmarks, here's what it takes to do nothing in
several languages:

rosuav at sikorsky:~$ cat repeat.sh
for i in {1..100}; do $@; done

rosuav at sikorsky:~$ time bash repeat.sh pike -e ';'

real 0m8.504s
user 0m7.928s
sys 0m0.436s
rosuav at sikorsky:~$ time bash repeat.sh python3 -c pass

real 0m3.094s
user 0m2.400s
sys 0m0.424s
rosuav at sikorsky:~$ time bash repeat.sh python2 -c pass

real 0m1.843s
user 0m1.136s
sys 0m0.488s

rosuav at sikorsky:~$ echo 'int main() {return 0;}' |gcc -x c -
rosuav at sikorsky:~$ time bash repeat.sh ./a.out

real 0m0.076s
user 0m0.004s
sys 0m0.012s

So, yeah. Pike's a poor choice and C's superb if you want to start up
and shut down real fast. Great. But as soon as those figures get
dwarfed by real work, nothing else matters. It's a rare situation
where you really need to start a program in less than 0.085 seconds.

ChrisA

[1] https://github.com/Rosuav/FrozenOST
[2] https://github.com/MikeiLL/appension
[3] https://github.com/Rosuav/LetMeKnow


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Cecil Westerhof
In reply to this post by Cecil Westerhof
Op Thursday 30 Apr 2015 11:10 CEST schreef Ben Finney:

>> I like it that the code is concise and clear. But also that the
>> performance is not bad.
>
> The former is a property of Python, which is a programming language.
> I agree with your assessment :-)
>
> The latter is not a property of Python; a programming language
> doesn't have runtime performance. Rather, runtime performance is a
> property of some specific *implementation* ? that is, the runtime
> Python machine.
>
> There are numerous Python runtimes, and they have different
> performance characteristics on different hardware and operating
> systems.
>
> <URL:https://wiki.python.org/moin/PythonImplementations>
>
> You might be talking about performance on CPython. But you might
> not! I don't know.

I understood when you did not explicitly mention it, it normally is
CPython. And in my case it is.

I should try it also in Jython, because Clojure runs in de JVM.


> Have you looked at PyPy ? Python implemented in Python ? and
> compared its performance?

Not yet. Busy hacking away. :-D

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Cecil Westerhof
In reply to this post by Marko Rauhamaa
Op Thursday 30 Apr 2015 12:16 CEST schreef Marko Rauhamaa:

> Ben Finney <ben+python at benfinney.id.au>:
>
>> The latter is not a property of Python; a programming language
>> doesn't have runtime performance. Rather, runtime performance is a
>> property of some specific *implementation* ? that is, the runtime
>> Python machine.
>>
>> There are numerous Python runtimes, and they have different
>> performance characteristics on different hardware and operating
>> systems.
>
> Still, Python has features that defy effective optimization. Most
> notably, Python's dot notation translates into a hash table lookup
> -- or worse.

Tail recursion would nice to have also.

> I currently carry three clubs in my professional golf bag: bash,

I published a Bash library:
    https://github.com/CecilWesterhof/BashLibrary

Maybe there is something useful in it for you. And if you need
something: let me know. No guarantees, but I could write it for you.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Michael Torrie
In reply to this post by Cecil Westerhof
On 04/30/2015 01:07 AM, Cecil Westerhof wrote:
> When I do that the computer is freezed a few times. That is a little
> less nice. Does not happen with Clojure when it gets an out of memory.

A system freeze is probably due to thrashing by your operating system as
a process (in this case Python) uses more and more memory, causing
massive swapping.  Clojure's heap, being a JVM-based language, is based
on JVM settings, so it may be maxing out at just a couple of GB.
Whereas Python will happily max out your swap if your program demands
the memory.



Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Cecil Westerhof
In reply to this post by Cecil Westerhof
Op Thursday 30 Apr 2015 16:03 CEST schreef Michael Torrie:

> On 04/30/2015 01:07 AM, Cecil Westerhof wrote:
>> When I do that the computer is freezed a few times. That is a
>> little less nice. Does not happen with Clojure when it gets an out
>> of memory.
>
> A system freeze is probably due to thrashing by your operating
> system as a process (in this case Python) uses more and more memory,
> causing massive swapping. Clojure's heap, being a JVM-based
> language, is based on JVM settings, so it may be maxing out at just
> a couple of GB. Whereas Python will happily max out your swap if
> your program demands the memory.

I just posted a message about that. This gets the problem also:
    l = range(int(1E9))

The problem is that after this other processes are swapped out and
have a bad performance. There is nothing that can be done about it?

So there is a positive point for working with the JVM. :-D

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Christian Gollwitzer
Am 30.04.15 um 18:11 schrieb Cecil Westerhof:

> Op Thursday 30 Apr 2015 16:03 CEST schreef Michael Torrie:
>
>> On 04/30/2015 01:07 AM, Cecil Westerhof wrote:
>>> When I do that the computer is freezed a few times. That is a
>>> little less nice. Does not happen with Clojure when it gets an out
>>> of memory.
>>
>> A system freeze is probably due to thrashing by your operating
>> system as a process (in this case Python) uses more and more memory,
>> causing massive swapping. Clojure's heap, being a JVM-based
>> language, is based on JVM settings, so it may be maxing out at just
>> a couple of GB. Whereas Python will happily max out your swap if
>> your program demands the memory.
>
> I just posted a message about that. This gets the problem also:
>      l = range(int(1E9))
>
> The problem is that after this other processes are swapped out and
> have a bad performance. There is nothing that can be done about it?
>
> So there is a positive point for working with the JVM. :-D
>

No, you understood this the wrong way. The JVM has a limit on the max
memory size on startup, which you can give by the -Xmx option on the
Oracle machine, so for instance

java -Xmx512M -jar myjar.jar

limits the memory that your program may consume to 512 megs, until the
JVM kills it. The standard limit is usually fairly low and probably
below your real memory, so the java program does not have the chance to
max out your memory and make your computer swap.

CPython, au contraire, uses all memory it can get from the OS. The OS
kills it if it uses too much. On Linux, you can set this limit yourself
using ulimit. The analogue to the java call would therefore be something
like

ulimit -m 512M
python mypython.py

If you set the ulimit to something smaller than your physical memory
size, you also cause the program to be killed before it can use up all
the memory and introduce swapping. For a fair comparison, you should set
both limits to the same value and see how far you can get.

        Christian


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Cecil Westerhof
Op Thursday 30 Apr 2015 19:59 CEST schreef Christian Gollwitzer:

> Am 30.04.15 um 18:11 schrieb Cecil Westerhof:
>> Op Thursday 30 Apr 2015 16:03 CEST schreef Michael Torrie:
>>
>>> On 04/30/2015 01:07 AM, Cecil Westerhof wrote:
>>>> When I do that the computer is freezed a few times. That is a
>>>> little less nice. Does not happen with Clojure when it gets an
>>>> out of memory.
>>>
>>> A system freeze is probably due to thrashing by your operating
>>> system as a process (in this case Python) uses more and more
>>> memory, causing massive swapping. Clojure's heap, being a
>>> JVM-based language, is based on JVM settings, so it may be maxing
>>> out at just a couple of GB. Whereas Python will happily max out
>>> your swap if your program demands the memory.
>>
>> I just posted a message about that. This gets the problem also:
>> l = range(int(1E9))
>>
>> The problem is that after this other processes are swapped out and
>> have a bad performance. There is nothing that can be done about it?
>>
>> So there is a positive point for working with the JVM. :-D
>>
>
> No, you understood this the wrong way. The JVM has a limit on the
> max memory size on startup, which you can give by the -Xmx option on
> the Oracle machine, so for instance
>
> java -Xmx512M -jar myjar.jar
>
> limits the memory that your program may consume to 512 megs, until
> the JVM kills it. The standard limit is usually fairly low and
> probably below your real memory, so the java program does not have
> the chance to max out your memory and make your computer swap.

Well, I did play a little with that. My experience is that this is to
high. For example by lowering it, a certain program did not swap
anymore. Sound counter intuitive, but not that strange if you know the
inner workings of the JVM.


> CPython, au contraire, uses all memory it can get from the OS. The
> OS kills it if it uses too much. On Linux, you can set this limit
> yourself using ulimit. The analogue to the java call would therefore
> be something like
>
> ulimit -m 512M
> python mypython.py

I use startup scripts for the different Java (or Scala, or Clojure)
programs to set the memory size. I should do that also for my (or
anothers) Python scripts.


> If you set the ulimit to something smaller than your physical memory
> size, you also cause the program to be killed before it can use up

Nope, the program gets a MemoryError and will not be killed. Much
better I think.


> should set both limits to the same value and see how far you can
> get.

You are right.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Steven D'Aprano-11
In reply to this post by Marko Rauhamaa
On Thu, 30 Apr 2015 08:16 pm, Marko Rauhamaa wrote:

> Still, Python has features that defy effective optimization. Most
> notably, Python's dot notation translates into a hash table lookup -- or
> worse.


Effective optimization may be difficult, but it isn't impossible. PyPy has a
very effective Just In Time optimizer.

http://speed.pypy.org/

PyPy currently ranges from approximately 1.25 times to 50 times faster than
CPython, with an average of over 7 times faster.

And now PyPy also has a version that runs without the GIL:

http://morepypy.blogspot.com.au/2015/03/pypy-stm-251-released.html


--
Steven



Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Steven D'Aprano-11
In reply to this post by Cecil Westerhof
On Thu, 30 Apr 2015 09:30 pm, Cecil Westerhof wrote:

> Tail recursion would nice to have also.

People coming from functional languages like Lisp and Haskell often say
that, but how many recursive algorithms naturally take a tail-call form?
Not that many.

I suppose that it would be nice if Python let you optionally use tail-call
optimization, but that might be tricky in practice.



--
Steven



Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Cecil Westerhof
Op Friday 1 May 2015 09:03 CEST schreef Steven D'Aprano:

> On Thu, 30 Apr 2015 09:30 pm, Cecil Westerhof wrote:
>
>> Tail recursion would nice to have also.
>
> People coming from functional languages like Lisp and Haskell often
> say that, but how many recursive algorithms naturally take a
> tail-call form? Not that many.

When I was playing with Scala there where a few cases where tail
recursion made a significant performance boost.

Is some time ago, so I do not remember which.


> I suppose that it would be nice if Python let you optionally use
> tail-call optimization, but that might be tricky in practice.

That is the difference between Scala and Clojure. Scala does it
silently, while by Clojure you have to say you want it. When a chance
breaks tail recursion Scala just compiles, but your code becomes
slower, Clojure does not compile. I prefer the Clojure variant.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Christian Gollwitzer
In reply to this post by Steven D'Aprano-11
Am 01.05.15 um 09:03 schrieb Steven D'Aprano:
> On Thu, 30 Apr 2015 09:30 pm, Cecil Westerhof wrote:
>
>> Tail recursion would nice to have also.
>
> People coming from functional languages like Lisp and Haskell often say
> that, but how many recursive algorithms naturally take a tail-call form?
> Not that many.

That is because tailcall optimization is used in functional languages
mainly as a replacement for loops. IOW, in non-functional languages you
can simply use an infinite loop to do the same (in most cases).

> I suppose that it would be nice if Python let you optionally use tail-call
> optimization, but that might be tricky in practice.
>

In Tcl 8.6 there is an explicit "tailcall something" command which calls
something without creating a new stack frame, i.e. it returns to the
caller after something returns. Useful sometimes, but more a
micro-optimization than a vital feature.

        Christian


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Gregory Ewing
In reply to this post by Steven D'Aprano-11
Steven D'Aprano wrote:

> People coming from functional languages like Lisp and Haskell often say
> that, but how many recursive algorithms naturally take a tail-call form?
> Not that many.

And the ones that do tend to be the ones that are
better expressed iteratively in Python. So Python
doesn't really need tail call optimisation.

Also, Guido has said that he doesn't like it,
because of the detrimental effect it has on stack
tracebacks.

--
Greg


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Cecil Westerhof
In reply to this post by Steven D'Aprano-11
Op Friday 1 May 2015 09:03 CEST schreef Steven D'Aprano:

> On Thu, 30 Apr 2015 09:30 pm, Cecil Westerhof wrote:
>
>> Tail recursion would nice to have also.
>
> People coming from functional languages like Lisp and Haskell often
> say that, but how many recursive algorithms naturally take a
> tail-call form? Not that many.

One example:
    def factorial(x, y = 1):
        return y if x == 1 else factorial(x - 1, x * y)

    def factorial_iterative(x):
        result = 1
        for i in range(2, x + 1):
            result *= i
        return result

Executing factorial(985) 100.000 times takes 54 seconds.
While executing factorial_iterative(985) takes 34 seconds.
Also you can not call factorial with a value that is much bigger
because of recursion depth. You can call factorial_iterative with
1.000.000.

I made also a version that simulates tail recursion:
    def factorial_tail_recursion(x):
        y = 1
        while True:
            if x == 1:
                return y
            y *= x
            x -= 1

This is that a lot less efficient as the iterative version. It takes 43
seconds. But it is a lot better as the normal recursive version: about
25%. The iterative version is about 25% more efficient as the tail
recursion version.

With larger values it decreases. Calculating onetime for 5.000.000
takes 117 and 131 seconds. Just 10% faster.

That is mostly because the tail recursion version starts multiplying
at the high end. I wrote a second version:
    def factorial_tail_recursion2(x):
        y = 1
        z = 1
        while True:
            if x == z:
                return y
            y *= z
            z += 1

This has almost the performance of the iterative version: 34 and 121
seconds.

So I made a new recursive version:
    def factorial_recursive(x, y = 1, z = 1):
        return y if x == z else factorial_recursive(x, x * y, z + 1)

But this take almost the same tame as the other. Probably the
recursive calls are more important as the multiplications.


I find factorial a lot cleaner code as factorial_iterative, so here
tail recursion would be beneficial.

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Marko Rauhamaa
Cecil Westerhof <Cecil at decebal.nl>:
> I find factorial a lot cleaner code as factorial_iterative, so here
> tail recursion would be beneficial.

I would just call math.factorial() and be done with it.

Note: Scheme is my favorite language and I use tail recursion all the
time. I also think eliminating tail recursion is such a low-hanging
fruit that even CPython should just pick it. However, I wouldn't use the
fibonacci sequence to justify anything at all about a programming
language.

It's not about performance (that's rarely a very useful argument when it
comes to Python). It's only that every now and then a very natural tail
recursion crops up and it's just silly to have to refactor perfectly
good code because of a stack overflow.


Marko


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Chris Angelico
On Sat, May 2, 2015 at 7:10 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Note: Scheme is my favorite language and I use tail recursion all the
> time. I also think eliminating tail recursion is such a low-hanging
> fruit that even CPython should just pick it. However, I wouldn't use the
> fibonacci sequence to justify anything at all about a programming
> language.

Actually, it isn't such low-hanging fruit. As has been mentioned in
this thread already, Guido is against anything that disrupts
tracebacks, and optimizing tail recursion while maintaining traceback
integrity is rather harder.

In the situations where it really is simple, you can always make the
change in your own code anyway. Often, the process of converting
recursion into tail recursion warps the code to the point where it's
abusing recursion to implement iteration anyway, so just make it
iterative.

ChrisA


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Cecil Westerhof
In reply to this post by Cecil Westerhof
Op Saturday 2 May 2015 10:26 CEST schreef Cecil Westerhof:

> Op Friday 1 May 2015 09:03 CEST schreef Steven D'Aprano:
>
>> On Thu, 30 Apr 2015 09:30 pm, Cecil Westerhof wrote:
>>
>>> Tail recursion would nice to have also.
>>
>> People coming from functional languages like Lisp and Haskell often
>> say that, but how many recursive algorithms naturally take a
>> tail-call form? Not that many.
>
> One example:
> def factorial(x, y = 1):
> return y if x == 1 else factorial(x - 1, x * y)
>
> def factorial_iterative(x):
> result = 1
> for i in range(2, x + 1):
> result *= i
> return result
>
> Executing factorial(985) 100.000 times takes 54 seconds.
> While executing factorial_iterative(985) takes 34 seconds.
> Also you can not call factorial with a value that is much bigger
> because of recursion depth. You can call factorial_iterative with
> 1.000.000.
>
> I made also a version that simulates tail recursion:
> def factorial_tail_recursion(x):
> y = 1
> while True:
> if x == 1:
> return y
> y *= x
> x -= 1
>
> This is that a lot less efficient as the iterative version. It takes
> 43 seconds. But it is a lot better as the normal recursive version:
> about 25%. The iterative version is about 25% more efficient as the
> tail recursion version.
>
> With larger values it decreases. Calculating onetime for 5.000.000
> takes 117 and 131 seconds. Just 10% faster.
>
> That is mostly because the tail recursion version starts multiplying
> at the high end. I wrote a second version:
> def factorial_tail_recursion2(x):
> y = 1
> z = 1
> while True:
> if x == z:
> return y
> y *= z
> z += 1
>
> This has almost the performance of the iterative version: 34 and 121
> seconds.
>
> So I made a new recursive version:
> def factorial_recursive(x, y = 1, z = 1):
> return y if x == z else factorial_recursive(x, x * y, z + 1)

Stupid me 'x == z' should be 'z > x'

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof


Reply | Threaded
Open this post in threaded view
|

Python is not bad ;-)

Marko Rauhamaa
In reply to this post by Marko Rauhamaa
Chris Angelico <rosuav at gmail.com>:

> Guido is against anything that disrupts tracebacks, and optimizing
> tail recursion while maintaining traceback integrity is rather harder.

Tail recursion could be suppressed during debugging. Optimized code can
play all kinds of non-obvious tricks with the execution frame.

> In the situations where it really is simple, you can always make the
> change in your own code anyway. Often, the process of converting
> recursion into tail recursion warps the code to the point where it's
> abusing recursion to implement iteration anyway, so just make it
> iterative.

While you shouldn't actively replace Python iteration with recursion, I
strongly disagree that naturally occurring tail recursion is abuse or
should be avoided in any manner.


Marko


12