News for hackers. Stuff that matters.
Re:JNumPy: Writing high-performance C extensions for Python ... by adgjlsfhk1 on Wednesday August 10, 2022 @12:15AM (#32407798)
Do you have any source on well written julia being any slower than well written C?
Re:JNumPy: Writing high-performance C extensions for Python ... by cycomanic on Wednesday August 10, 2022 @02:56AM (#32408569)
I did a comparison of Julia vs numpy, cython and pythran [1] some time ago, for a typical dsp routine we use in our work, and Julia was quite a bit slower than the alternatives. Now I'm by no means a Julia expert so I might have missed an optimisation opportunity (although I posted this and nobody could point to something obvious) , however the whole advertisement behind Julia is that one gets essentially C speed with the ease of Python. That's actually my main gripe with Julia, the community advertises it as something that it isn't. I think otherwise some really cool stuff is happening in Julia Land.

[1] https://jochenschroeder.com/blog/articles/DSP_with_Python2/

Re:JNumPy: Writing high-performance C extensions for Python ... by sundarurfriend on Wednesday August 10, 2022 @08:55AM (#32411104)
> (although I posted this and nobody could point to something obvious)

For someone outside the field of DSP, please note that your post gives no context to the problem, nor to the input data. It's pretty hard to optimize something when we don't understand the structure of the problem or the input, and what exactly the code is trying to accomplish.

I remember taking a look at the post when it was posted some time ago, and having to give up pretty quickly because it was hard to make sense of it (in any language), and it seemed like I had to install specific Python libraries to (- hopefully, if everything works -) generate the input data. That takes it beyond "fun little optimization diversion to the day" to something that feels like work.

(Also, to anyone who hasn't read the post, I'll note that somebody did point to something obvious, as mentioned in the update to the post - a single suggestion that only needed superficial understanding of the code, that lead to a 3x speedup from OP's original Julia code.)

FWIW, someone with some domain knowledge on the Julia Discourse [1] was able to generate the input data, and posted code that they claim should be faster. They only mention the run time of the new code - 9 ms - so I don't know if, and by how much, it is faster in comparison. (Note that the code uses global variables at the end - which is fine there since they're using `@btime` and `$`s, but if using `@timed` instead, those should be enclosed in a `main` function or a `let` block.)

[1] https://discourse.julialang.org/t/this-post-claims-that-juli...

Re:JNumPy: Writing high-performance C extensions for Python ... by webshit2 on Wednesday August 10, 2022 @04:03AM (#32409046)

  wxy[:,:,k] += mu*conj(err[i,k])*X
This should probably be

  wxy[:,:,k] .+= mu*conj(err[i,k]).*X
so allocations are avoided. This doubled the speed for me, although I don't what size inputs are realistic. The @benchmark and @profile macros are good for this stuff.
Re:JNumPy: Writing high-performance C extensions for Python ... by crabbygrabby on Wednesday August 10, 2022 @06:36AM (#32409918)
Why are you trying to optimize for performance if you don't have a goal in mind?
Re:JNumPy: Writing high-performance C extensions for Python ... by sundarurfriend on Wednesday August 10, 2022 @12:03PM (#32414240)
> the whole advertisement behind Julia is that one gets essentially C speed with the ease of Python. That's actually my main gripe with Julia, the community advertises it as something that it isn't.

I partially agree with this - that statement is true some of the time with some code, but in general there's a lot of nuance to that. (Though, I haven't seen that as a general claim much apart from in the initial "Why we created Julia" post describing what they were hoping to achieve; I more often see specific claims where people were able to port their scientific code into Julia that looks like Python and runs like C. But that may just be us having different bubbles online.)

My partial disagreement is from the fact that oftentimes, the "ease of Python" seems to get interpreted by Python coders as "as easy as Python is to me today" rather than "as easy as it was when I was a beginner" i.e. they expect to be able to port their Python knowledge directly somehow. Python has its own quirks and pitfalls, and over the first few months of people learn to work with/around them and write idiomatic Python code (and then it becomes second nature, and we often even forget that we had to learn to do this). Given a similar on-ramp, people can easily write Julia code that's at least comparable to C in terms of speed, even if it doesn't always match it.

But when you do need to match or exceed C, Julia starts asking for more in-depth knowledge of allocations, types and their performance characteristics, and the code starts deviating away from looking quite as readable as Python. Hopefully, you only need that in some deep internal functions or packed away in a library, but it _is_ a big caveat to that statement.

Re:JNumPy: Writing high-performance C extensions for Python ... by salty_biscuits on Wednesday August 10, 2022 @03:15AM (#32408684)
I only had a quick look, but I would guess the slicing is what makes it slower than you would expect. Julia copies on an array slice, but you can use the @view macro or do some loops with a preallocated array to make it faster.
Re:JNumPy: Writing high-performance C extensions for Python ... by cycomanic on Wednesday August 10, 2022 @03:39AM (#32408852)
Thanks I'll have a look and post and update sometime later.
Re:JNumPy: Writing high-performance C extensions for Python ... by meta2meta on Wednesday August 10, 2022 @03:35AM (#32408821)
You should run Julia code directly to benchmark instead of calling it in Python through pyjulia.
Re:JNumPy: Writing high-performance C extensions for Python ... by cycomanic on Saturday August 13, 2022 @01:30PM (#32451951)
Actually I benchmarked inside Julia as well and didn't see a difference so left it like this.
Re:JNumPy: Writing high-performance C extensions for Python ... by jakobnissen on Wednesday August 10, 2022 @01:35AM (#32408118)
Don't be unreasonable. Numerical Julia code is just as fast, precisely because it's easy to design efficient high-level APIs and let the compiler optimise it.

Try timing Julia's IO, or it's string processing, or it's hash tables, or its array allocation or it's GC - you know, nearly everything other that mathematical operations on floats or ints. Nearly everything leaves performance on the table.

Sure Julia is fast, but not on a C level in these areas. There is still lots of room for optimisation, though.

Re:JNumPy: Writing high-performance C extensions for Python ... by jkrubin on Wednesday August 10, 2022 @01:54AM (#32408210)
What’s your point?
Re:JNumPy: Writing high-performance C extensions for Python ... by anewhnaccount2 on Wednesday August 10, 2022 @02:09AM (#32408297)
The point is that these things (string processing/hash tables) are typically done using custom/tuned code in C and so would be faster than the generic, and not particularly carefully tuned Julia implementations.

However, given this library seems to be about passing NumPy arrays to/from Julia, the application is almost certainly to expose numerical stuff from Julia to Python. Basically, the use case is similar to Numba, but it works by producing a C-extension from a Julia system image (if I understand correctly). The main advantage over something like Numba would be access to the Julia ecosystem.

Re:JNumPy: Writing high-performance C extensions for Python ... by jakobnissen on Wednesday August 10, 2022 @02:25AM (#32408383)
The point is that when adgjlsfhk1 doubts that Julia is slower than C, he does so from a background where "data" is a synonym for arrays of numbers, and "computing" means processing these arrays. Julia shines here, precisely because the people developing Julia have a similar focus.

In contrast, if your performance bottlenecks are IO, parsing and hash table operations, then Julia's performance will be in par with Python and get absolutely crushed by Rust (I have less experience with C, but would imagine Rust and C are about equally fast).

The major difference with Python is that Julia's implementation of these things _could_ be fast in a way that Python's can't. They just aren't.

Re:JNumPy: Writing high-performance C extensions for Python ... by ChrisRackauckas on Wednesday August 10, 2022 @06:49AM (#32409998)
Some of these are just safety-by-default things. For example, IO in Julia is thread-safe by default, which I think is a good idea because safety-first programming is good for say throwing `print` into a threaded loop written in the REPL. Here for example, https://github.com/aaronang/stl-benchmark/pull/3, was a case where Julia saw a performance hit from C++ and I was curious and tracked it down to this locking-by-default behavior. I'm not sure of a better way of handling it: the C/C++ behavior of not locking by default would make doing things correctly simply would be very hard to use (and is very hard in those languages).

Though I agree parsers haven't gotten much love in Julia. That said, this repo is saying it's for implementing NumPy extensions, and I don't think NumPy has many parsers it's using.

Re:JNumPy: Writing high-performance C extensions for Python ... by guimplen on Wednesday August 10, 2022 @08:10AM (#32410595)
in posix (so in any reasonable C implementation) printf is thread-safe by default, so your comment doesn't make much sense.
Re:JNumPy: Writing high-performance C extensions for Python ... by adgjlsfhk1 on Wednesday August 10, 2022 @02:34AM (#32408434)
on par with python is way off. Julia in these areas definitely isn't fully optimized, but the performance will be much closer to something like Java for these cases. a dict in Julia will still have 1 pointer indirect compared to the python one where all your objects have 16 bytes overhead and an extra pointer indirection.
Re:JNumPy: Writing high-performance C extensions for Python ... by jakobnissen on Wednesday August 10, 2022 @02:43AM (#32408479)
Python's dicts are way more optimised and have an implementation closer to Dictionaries.jl.

Anyway, I think it might be a good exercise for me to make a repo with a few test cases of where I think Julia could use some optimisations, and compare to Rust and Python.

Re:JNumPy: Writing high-performance C extensions for Python ... by bigdict on Wednesday August 10, 2022 @12:25AM (#32407856)
Re:JNumPy: Writing high-performance C extensions for Python ... by webshit2 on Wednesday August 10, 2022 @01:07PM (#32415313)
It says right there: "Always look at the source code"

fannkuch-redux, n-body, spectral-norm, reverse-complement: the fastest C wins by a large margin because it's an unreadable mess of vector intrinsics. If you really wanted to, you can do that in Julia too (and it might read better). Julia looks pretty even with C in the ones that were faithfully translated.