Is Python really this slow compared to Kotlin/Java...
# science
t
Is Python really this slow compared to Kotlin/Java unless you bring in library support?
a
Yes. Unless you use numpy, it is up to 30x difference. And even with numpy, you need to be careful to use it in a very specific way.
1
t
Okay... that's what I knew before but definitely not to this magnitude. As discussed, I'm putting in NumPy implementations later too with this OReilly class I'm making... maybe I'll even use kmath as well, since I've been throwing Kotlin versions in there too. But I still like having these super vanilla implementations without linear algebra, even if performance is terrible. It breaks the linear algebra barrier in teaching how machine learning works, and using simple hill climbing breaks the barrier for learning calculus. However, I know linear algebra is going to be necessary for neural networks because they are so messy without them.
a
Well, we have to use vanilla for kotlin-multiplatfotm. Fjor example this one: https://github.com/mipt-npm/kmath/blob/fcc6269ee8c7604c0e0ef3e67190029ea5e24f6e/kmath-core/src/commonMain/kotlin/scientifik/kmath/linear/MatrixContext.kt#L62 The performance is quite good in some cases but not stellar in general (you need platform optimization for really good performance).
t
@altavir yeah I was wondering about that and how that would be implemented. I've been exploring kmath source code a little, it's nice to see it's pretty readable at least.
b
Did you try sklearn implementations? They will likely be much much faster. https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html
Python is slow if you have to exit from numpy. If you do everything in the numpy world it is really fast (and can take advantage of openblas and so on)
a
It is general knowledge that python with numpy is fast, but lately I am starting to doubt the statement. There are two problems with python even with numpy. • The memory management is not as effective as, say, in JVM. So the simple java program runs faster when it works with a lot of allocations. • The architectural limitations of numpy usually force to use very ineffective practices. The recent example is a code from my phd student. He is quite proficient with numpy, but rewriting it in kotlin, increased performance about 10 times. Probably there was a hard-to-detect bottleneck somewhere.
b
Absolutely.
Numpy is fast while you stay in numpy world. Anytime you have to exit back to python, you take a huge performance hit.
where pure python can be faster than numpy. Anyway we should get Kotlin everywhere, no questions asked