News for hackers. Stuff that matters.
Re:Yes, Python is Slow, and I Don’t Care by agentgt on Friday September 08, 2017 @09:17AM (#15199914)
Unless we are talking like circa 1999 I don't think I have heard a complaint yet that Python is slow. I'm curious who or where the author heard that from (not specifically the people themselves but the domain they are in).

What I have heard complaints about Python are (and I don't agree with all these points):

* Its not statically typed

* The python 2/3 compatibility

* It has some design flaws: GIL, variable assigning, mutable variables, lambdas, indentation (I don't agree with all these but this is complaints I have heard).

* The plethora of packaging (ie its not unified)

I guess one could argue its slow because it can't do concurrency well but that really isn't raw speed.

Then the author started comparing string processing of programmer time from a study which... doesn't help the authors point at all.

* Python has and will always be fast at string processing and most people know this

* The people that complain about python speed are almost certainly not doing string processing

* I have serious questions about the study in general (many languages have changed quite a bit since then)

Re:Yes, Python is Slow, and I Don’t Care by nerdponx on Friday September 08, 2017 @09:43AM (#15200065)
For some data processing tasks Python can be brutally slow, especially text processing. NumPy is only fast because it's written in C and offloads hard numerical calculations to BLAS.
Re:Yes, Python is Slow, and I Don’t Care by agentgt on Friday September 08, 2017 @10:47AM (#15200558)
Yes but at least Python has very good native access to libraries.

For example I am sure Java is faster at numerical processing than pure Python at least with Python you have the option of writing in native. Python has really good C interop and it maybe considered by some not part of the language but I consider that it is.

Yeah you could write native code for Java via JNI but its not easy (granted its been a while) and for reasons I can't remember why but JNI is slow (probably moving stuff on and off the heap... I can't recall).

Re:Yes, Python is Slow, and I Don’t Care by AstralStorm on Friday September 08, 2017 @12:04PM (#15201338)
Decent, not very good. Ctypes is slow, cython requires extra boilerplate to become performant. Python/C API is even more annoying than that. (Compared even to something like JNI.)
Re:Yes, Python is Slow, and I Don’t Care by pg314 on Friday September 08, 2017 @09:43AM (#15200066)
> I'm curious who or where the author heard that from (not specifically the people themselves but the domain they are in).

In the telecom domain, I've dealt with data big enough that Python wasn't really feasible. Think 100 of millions of records in CSV format that need to be parsed and processed. Doing that in Python is going to be painful.

Re:Yes, Python is Slow, and I Don’t Care by agentgt on Friday September 08, 2017 @10:30AM (#15200402)
Its interesting you mention CSV as I had an issue with CSV with Java many years ago.

I was trying to cut a couple of columns out of a CSV and tried to write it in Java and for some reason either the CSV library was slow or maybe I didn't have the right buffered input but I ended up writing a python script that outperformed my Java code (which reminds me I meant to revisit that).

Anyway here is the python code (it might not be the exact code I used as I didn't update it but whatever):

https://gist.github.com/agentgt/1383185

Re:Yes, Python is Slow, and I Don’t Care by classybull on Friday September 08, 2017 @10:16AM (#15200292)
Python is insanely fast at data processing and analysis because it has very fast libraries.

As a matter of fact, don't know if you've heard, but data processing it kind of like.. Python's thing...

Re:Yes, Python is Slow, and I Don’t Care by mattkrause on Friday September 08, 2017 @10:41AM (#15200495)
You're violently agreeing with each other.

Python itself can be pretty slow. Doing image processing on data stored as list-of-lists-of-integers would be brutally slow.

On the other hand, numpy is an import away, and it can be quite fast, especially if it's been built with an optimized BLAS/ATLAS, etc.

Re:Yes, Python is Slow, and I Don’t Care by AstralStorm on Friday September 08, 2017 @12:07PM (#15201360)
By blazingly fast you mean 100x slower than C++ equivalent and only 20x slower is you're very careful to avoid accidental copies.

For reference, MATLAB is about 30x slower with no special care. Pure Java on Hotspot was 5x slower except it dies on big data input due to very slow GC and goes to 50x slow.

Source: handled big audio data from hdf5 database, gigabytes sized. C++ equivalent had no vectorization or magic BLAS or anything.

Re:Yes, Python is Slow, and I Don’t Care by joshuamorton on Friday September 08, 2017 @12:38PM (#15201701)
As I'll often say to these comments, then you're doing things wrong. Numpy code can be written to never leave the numpy sandbox, and at that point it should be as fast or faster than naive c++ (because you'll be getting SSE and stuff for free).

There's a reason almost all deep learning is done in python.

Re:Yes, Python is Slow, and I Don’t Care by AstralStorm on Monday September 11, 2017 @02:03AM (#15216323)
The first time you have to return into python all the gains evaporate. As I said, we improved python code a few times and it never even got near. What it did was a bunch of convolutions, dot products, multiplications, fft, gamma probability equations. Even vector comparisons used numpy. Used direct hdf5 interface numpy has and fast view interface for overlaps.

Numba choked on the code by the way and crashed unless views were removed. And then it produced much worse performance.

Re:Yes, Python is Slow, and I Don’t Care by pg314 on Friday September 08, 2017 @02:52PM (#15202971)
Not all data is a good fit for Numpy: some data is non-numeric or not a homogenous array.

> There's a reason almost all deep learning is done in python.

The heavy-lifting in e.g. TensorFlow is done in C++. Bindings to Python make sense because it is one of the few sanctioned languages inside Google, and it is widely used outside of Google and easy to pick up.

Re:Yes, Python is Slow, and I Don’t Care by joshuamorton on Friday September 08, 2017 @11:54PM (#15205483)
>The heavy-lifting in e.g. TensorFlow is done in C++. Bindings to Python make sense because it is one of the few sanctioned languages inside Google, and it is widely used outside of Google and easy to pick up.

That's exactly the same as with numpy. I'm not sure what your point is. C++ is also one of the few sanctioned languages inside google, as is Java.

>Not all data is a good fit for Numpy: some data is non-numeric or not a homogenous array.

I'm curious what kind of data you're working with that can't be represented and effectively transformed in a tensor (numpy array).

Re:Yes, Python is Slow, and I Don’t Care by pg314 on Saturday September 09, 2017 @05:05AM (#15206196)
> That's exactly the same as with numpy. I'm not sure what your point is.

I was replying to "there's a reason why...". You didn't specify that reason, so from the rest of your comment I took it to mean that Python (with numpy) was fast and good enough to write deep learning stuff. That doesn't seem to be the case for TensorFlow.

> I'm curious what kind of data you're working with that can't be represented and effectively transformed in a tensor (numpy array).

I'm not intimately familiar with the internals of numpy, but my understanding is that the basic data structure is a (multi-dimensional) array of values (not pointers). That leads to a number of questions.

If you have an array of records (dtype objects), and one of the fields is a string, am I correct that each element needs to allocate memory to hold the longest possible value that can occur for that field? What if that is not known beforehand?

How do you deal with optional fields (e.g. int or null)? Do you need to add a separate boolean to indicate null?

How do you deal with union types, e.g. each record can be one of x types, do you make a record that has a field for each of the fields of those x types? Do those fields take up space?

Re:Yes, Python is Slow, and I Don’t Care by joshuamorton on Sunday September 10, 2017 @06:31PM (#15214645)
>You didn't specify that reason, so from the rest of your comment I took it to mean that Python (with numpy) was fast and good enough to write deep learning stuff. That doesn't seem to be the case for TensorFlow.

Tensorflow tensors are numpy arrays, or are transparently viewable as such.

>If you have an array of records (dtype objects), and one of the fields is a string, am I correct that each element needs to allocate memory to hold the longest possible value that can occur for that field? What if that is not known beforehand?

Yes, although you can also store numpy arrays of pyobjects, which are arrays of pointers. You'll be able to vectorize the code, but you won't get the same performance improvements as with a normal numpy array, because that same level of performance isn't possible with an array of pointers.

Note that for most machine learning applications, you'd preprocess your string into a vector of some kind.

>How do you deal with optional fields (e.g. int or null)? Do you need to add a separate boolean to indicate null?

Yes, but I'm not sure when you'd do that. That is, again in most machine learning applications you'd be representing things as one-hot arrays or as some kind of compressed high dimensional position vector, where 0 would represent a lack of presence of some thing.

>How do you deal with union types

dt = np.dtype((np.int32,{'real':(np.int16, 0),'imag':(np.int16, 2)})

is a 32 bit int that can also be accessed as a 16 bit complex number via .real and .imag.

Re:Yes, Python is Slow, and I Don’t Care by pg314 on Friday September 08, 2017 @02:45PM (#15202914)
> Python is insanely fast at data processing and analysis because it has very fast libraries.

It doesn't have fast libraries for everything. E.g. for the use case I brought up, it doesn't (or at least none that I could find at the time). Not all data are homogenous arrays that fit in numpy.

And are those libraries written in Python? Or in C because Python is too slow?

If you take your reasoning, any language that can bind to C (which is pretty much any language) is as fast as C. That is not very helpful when comparing the speed of languages. A slower language will force you to drop into C sooner than a faster one.

> As a matter of fact, don't know if you've heard, but data processing it kind of like.. Python's thing...

Things like audio and video codecs, or crypto code aren't written in Python.

Re:Yes, Python is Slow, and I Don’t Care by dahart on Friday September 08, 2017 @10:19AM (#15200307)
I'd agree with all the complaints you list, and I love Python, but it is definitely kinda slow. I'd put the speed of python below several of the items on your list in terms of priorities I care about.

I've done a lot of image processing in Python using libraries like PIL, numpy and opencv. Doing per-pixel operations is extremely easy in PIL - improving my dev time, but CPU wise the slowest of the bunch. I love prototyping that way, but I have to move to numpy or opencv or another language to speed it up. A recent program to do a slightly complicated color transform on a 512x512 image was taking me over 60 seconds with PIL. It was 5-10 seconds in JavaScript, and less than 1 second using numpy.

Re:Yes, Python is Slow, and I Don’t Care by et2o on Friday September 08, 2017 @10:24AM (#15200354)
Wouldn't numpy be the default choice for this type of work in Python?
Re:Yes, Python is Slow, and I Don’t Care by dahart on Friday September 08, 2017 @10:41AM (#15200496)
Yes, if you care a lot about performance. But there's a cognitive cost to numpy for me because I'm not completely fluent. It's much more difficult to prototype something, so I usually wait until I've iterated on a prototype and I know exactly what I want to do before I dive into numpy.

Numpy also has an inside-out order of operations compared to using vanilla Python, the reason it's fast is because you put the inside of the loop on the outside of the code. That can make it really hard to iterate on structural changes in your code. You can lose a lot of the dev time advantages by going numpy first.

Also a lot of people don't consider numpy to be a fair comparison when talking about Python's language performance. Because it's optimized compiled code underneath the Python binding, it's not representative of the speed of the Python interpreter.

Re:Yes, Python is Slow, and I Don’t Care by bane on Friday September 08, 2017 @10:55AM (#15200636)
Python is very developer productivity friendly, but degrades performance in weird ways and the methods to increase performance don't always make lots of obvious sense and often are the "idiomatic" way.

I haven't looked into the guts myself, but I'd bet that for similar or equivalent operations, the way the different syntaxes are handled under the hood are wildly different. e.g. function calls are much faster than method calls on objects.

Optimized pure python can look very ugly and non-idiomatic.

Re:Yes, Python is Slow, and I Don’t Care by rockostrich on Friday September 08, 2017 @09:27AM (#15199970)
Most comparisons of popular web development languages will show a table where every other language out performs Python (except maybe Ruby) [1]. In reality, like the author pointed out, the benchmarks don't really matter when you consider network calls.

[1] https://www.techempower.com/benchmarks/

Re:Yes, Python is Slow, and I Don’t Care by nhumrich on Friday September 08, 2017 @10:03AM (#15200209)
Author here. I mostly work with a lot of Java devs. They are hesitant to try python because "java is faster". Some use the static typing excuse, but oddly, the most common reason I hear people wont do python is because its "too slow". Perhaps I live in a weird bubble, but that's the motivation behind the article.
Re:Yes, Python is Slow, and I Don’t Care by agentgt on Friday September 08, 2017 @10:22AM (#15200333)
I'm a Java dev (and the one you replied to) and in the past when I didn't work at my own company the real reason Python was often not picked was because it was considered "a toy language" or "scripting language" (ie hard to maintain) and also because it didn't have vendor support like Java did. Of course this was like 8-10 years ago.

I still program and sadly prefer writing code in Java over Python even though I have tons of Python scripts in my ~/bin that I have written over the years. Mainly because I just know the ecosphere better.

And thats what most people should just admit why they choose a particular programming language. As long as its good enough... better... is just what you know more of... the cost of learning something new just to replace something new for marginal speed increase isn't worth it.

So when people say Java is slow to learn, not cool or slow to develop in because the language is verbose... I say just like you: "I Dont' Care" because learning the whole ecosphere of another language is goddamn expensive (that is languages are relatively easy to learn but all the libraries, best practices and tools is another thing entirely).

Re:Yes, Python is Slow, and I Don’t Care by nhumrich on Friday September 08, 2017 @10:28AM (#15200383)
Thats a very fair argument. But lets say hypothetically that language X was more "productive" than language Y. Wouldnt it be a worthwhile "investment" to learn X seriously? Sure it might slow you down for a year, but after that it pays dividends.
Re:Yes, Python is Slow, and I Don’t Care by agentgt on Friday September 08, 2017 @10:39AM (#15200481)
Yes of course and for my own personal projects I will take the time often to learn new languages (e.g. Rust).

However when dealing with a team often with an existing code base getting everyone on board with "X" while potentially porting to "X" is fairly expensive. Its generally only worthwhile because of actual technical limitations and not productivity. Even the promise of safety (ie prevention of bugs) I would probably consider higher than productivity (although I suppose that is in way productivity).

Regardless productivity is really hard to measure and especially predict unlike other technical things.

Re:Yes, Python is Slow, and I Don’t Care by Can_Not on Friday September 08, 2017 @11:18AM (#15200845)
But if it's all unjustified hype, unmentioned drawbacks, and empty promises, then your losses can be extraordinary.
Re:Yes, Python is Slow, and I Don’t Care by wheaties on Friday September 08, 2017 @09:23AM (#15199948)
Oh, I've heard "Python is slow" from Node folks, Java folks, Scala folks (in terms of productivity) and of course, die hard C++ fans. I'm a fan of Python but i also like languages with a stronger FP bent (and statically typed.)
Re:Yes, Python is Slow, and I Don’t Care by agentgt on Friday September 08, 2017 @09:28AM (#15199972)
> Oh, I've heard "Python is slow" from Node folks, Java folks, Scala folks (in terms of productivity)

I have hard time believing Java folks made an argument on productivity unless you meant for that parenthetical statement to be only applied to Scala. Regardless productivity is a far more complicated than raw speed (particularly if you get into maintenance as some languages are easy to pump out code but harder to maintain... oh perl.. the pain...).

And yeah I have seen the C++ folks complain about Python but this in my experience has been video game programming which is clearly not the domain the author is in or talking about (I think given the microservice discussions). Hence why I would like to know more about these folks complaining.

Re:Yes, Python is Slow, and I Don’t Care by Acalyptol on Friday September 08, 2017 @09:53AM (#15200122)
Could you elaborate on what are the complaints you've heard about "variable assigning, mutable variables, lambdas"? Just curious.
Re:Yes, Python is Slow, and I Don’t Care by agentgt on Friday September 08, 2017 @10:57AM (#15200652)
It unclear if you are introducing a new variable to the scope or mutating an existing one.

    # Am I defining foo for the first time or am I mutating
    foo = "stuff"
Python if I recall in the OOP sense combats this with requiring self.foo but this is not the case with other forms of lexical scoping such as loops with in loops etc.

And of course you can't define constants.

IIRC for lambdas its that they can't span multiple lines and I believe they are getting removed (I'm not a python 3 expert)?

Re:Yes, Python is Slow, and I Don’t Care by odiroot on Friday September 08, 2017 @10:03AM (#15200210)
> * Its not statically typed

Yes, that's why we use it.