"it's slow" "just write a C extension" "but then why use Python?"
Because beautiful control structures and a REPL ?
Why Python Is Slow: Looking Under the Hood
81–90 of 156 posts
Re: Why Python Is Slow: Looking Under the Hood
#82Please forgive my ignorance, but why can't Python just be compiled into assembly like C? The compiler should be able sift through the code and see that "x=1" is an integer, or converts to a float when "x=x+1.0". If I'm doing something like counting from 1 to 10 and summing the count, why can't this be compiled to run the same speed as C? Obviously since it's interpreted, but when I'm done with development, why can't…
Re: Why Python Is Slow: Looking Under the Hood
#83Please forgive my ignorance, but why can't Python just be compiled into assembly like C? The compiler should be able sift through the code and see that "x=1" is an integer, or converts to a float when "x=x+1.0". If I'm doing something like counting from 1 to 10 and summing the count, why can't this be compiled to run the same speed as C? Obviously since it's interpreted, but when I'm done with development, why can't…
It provides facilities for all of python abilities regardless of what the program is going to do.
Now, there are subsets of python people have developed that can be compiled and all sorts of other stuff.
Beyond that, I'd just say "all this stuff sounds easy but gets much harder when you actually have to program it".
"Why can't it just know!" is the programmer's lament.
Re: Why Python Is Slow: Looking Under the Hood
#84Almost all these points apply to javascript as well. Javascript, however, is not really slow anymore. http://benchmarksgame.alioth.debian.org/u64/benchmark.php?te...
Sort of.
The article talks about why CPython, an implementation of the Python language, is slow. You're right that a naive implementation of Javascript would have many of the same problems, and indeed many of the first implementations of JS did have these problems. But due to heated competition, Mozilla, Google, Apple, and Microsoft have invested a huge amount of top-quality engineer-years into optimizing their implementations of JS.
Implementations of Python, in contrast -- and CPython in particular -- have not received the same sort of love.
I largely agree with the notion that the speed of modern JS engines serves as an existence proof that Python could be similarly fast.
Re: Why Python Is Slow: Looking Under the Hood
#85Please forgive my ignorance, but why can't Python just be compiled into assembly like C? The compiler should be able sift through the code and see that "x=1" is an integer, or converts to a float when "x=x+1.0". If I'm doing something like counting from 1 to 10 and summing the count, why can't this be compiled to run the same speed as C? Obviously since it's interpreted, but when I'm done with development, why can't…
Re: Why Python Is Slow: Looking Under the Hood
#86Please forgive my ignorance, but why can't Python just be compiled into assembly like C? The compiler should be able sift through the code and see that "x=1" is an integer, or converts to a float when "x=x+1.0". If I'm doing something like counting from 1 to 10 and summing the count, why can't this be compiled to run the same speed as C? Obviously since it's interpreted, but when I'm done with development, why can't…
Re: Why Python Is Slow: Looking Under the Hood
#87Almost all these points apply to javascript as well. Javascript, however, is not really slow anymore. http://benchmarksgame.alioth.debian.org/u64/benchmark.php?te...
What's easy to lose track of under the steady stream of headlines about this microbenchmark going 5x faster and that microbenchmark going 10x faster and this one microbenchmark being "faster than C" is that Javascript is still a fairly slow language, though. Flick that to compare Javascript vs. C: http://benchmarksgame.alioth.debian.org/u64/benchmark.php?te...
It has come a long way. It is, of course, still usable for many things. But it is still an interpreted language, and my guess is that we've basically plateaued on performance for Javascript, and it's important to remember that it isn't as "fast as C" in any useful sense of the term. And for that matter, to get even that performance out of JS seems to require you to write a very specialized subset of JS that will tickle the optimizers correctly. In practice I suspect you will get even worse deltas over C, which, for all of its many failure modes, does make it often easier to do the fast thing than the slow thing, usually regardless of the correctness of either of them.
(That's why we have asm.js, after all. You can't have both of "Javascript is as fast as C" and "asm.js is several times faster than Javascript", because the composition of both statements is absurd.)
Re: Why Python Is Slow: Looking Under the Hood
#88Please forgive my ignorance, but why can't Python just be compiled into assembly like C? The compiler should be able sift through the code and see that "x=1" is an integer, or converts to a float when "x=x+1.0". If I'm doing something like counting from 1 to 10 and summing the count, why can't this be compiled to run the same speed as C? Obviously since it's interpreted, but when I'm done with development, why can't…
But things get much more complex very fast, and then the compiler requires the programmer to tell it what types things are.
Then there is the approach taken by JavaScript engines like V8. Run the code for a while, see what the types of variables are, then compile a customized, much more efficient version of the code to speed things up. Still need a fallback, though, in case a totally different kind of value gets stuck in a variable than the type at the time the optimized code was generated. Nothing in JavaScript (or Python) prevents a programmer from assigning any kind of value into a variable, though, so the engine must be prepared for the types of variables to change in unexpected ways.
(The reason the state of the art for these techniques is in the JavaScript engines is because the organizations behind the major browsers pour so many resources into making them fast.)
Now, if you can figure out a way to always predict the types of every variable in a JavaScript or Python program without actually running it, pretty much any CS program would be willing to give you a tenured position on their faculty on the spot. :)
Re: Why Python Is Slow: Looking Under the Hood
#89Please forgive my ignorance, but why can't Python just be compiled into assembly like C? The compiler should be able sift through the code and see that "x=1" is an integer, or converts to a float when "x=x+1.0". If I'm doing something like counting from 1 to 10 and summing the count, why can't this be compiled to run the same speed as C? Obviously since it's interpreted, but when I'm done with development, why can't…
Re: Why Python Is Slow: Looking Under the Hood
#90Heh, Python is slow: in order to write fast Python, you have to write in C and conform to the FFI. I vastly prefer to use tools which don't come misshapen out of the box, personally.
Different tools for different jobs. Being able to develop code quickly is a huge win that Python delivers. Sometimes that makes Python the right tool, e.g. Youtube. Other times slow execution speed makes it the wrong choice. [0] http://www.gooli.org/blog/youtube-runs-on-python/