Julia: Performance Tips

(docs.julialang.org)

47 points | by tosh 3 days ago

5 comments

  • muragekibicho 2 minutes ago
    My PhD advisor demanded I use Julia for my dissertation. Back then I was a junior programmer so Julia's one-based indexing absolutely threw me off.

    In retrospect, the PhD experience was miserable and using Julia contributed to my 'death by a thousand cuts'.

  • stabbles 16 minutes ago
    Back in the day I had trouble convincing my C++ friends to give Julia a try, because Julia's garbage collector was a showstopper. But if you follow these performance tips related to pre-allocation, in-place mutation, avoiding temporary allocations (and maybe avoiding cycling references), you don't struggle with GC performance issues.

    Looking back, I think the tooling Julia had from the start, combined with the REPL, made it actually really nice to fix these performance issues. Much better than compiling and linking a binary and running it through some heap profiler tool. You could simply do

        julia> @time f()
          x.y seconds (N allocations: M bytes)
    
    and iteratively improve part of the code base instead of profiling the entire application.

    (To be fair: back then escape analysis was not implemented in the compiler, and it was hard to avoid silly allocations)

  • Hasnep 50 minutes ago
    There is also a section of Modern Julia Workflows [1] about optimisation that gives helpful, practical advice.

    [1] https://modernjuliaworkflows.org/optimizing/

  • jdiaz97 26 minutes ago
    I love Julia. It's a fun language, LLMs write it better than they write Python.
  • pachico 2 hours ago
    Legitimate and honest question: in which circumstances would you choose Julia over more mainstream alternative like Go?
    • qalmakka 9 minutes ago
      My two cents: while Julia is arguably more complex than Go, it's type system and especially its data types (N-dimensional arrays, ...) make it way more suited anything that needs to process complex data, or do anything that's even closely related to geometry. `.|>` is wonderful and makes functional-like code easier, and that's just the tip of the iceberg, macros are also beautiful and absurdly useful. Also LLVM more often than not generates faster code than the Go backend, albeit the slowdown at startup in Julia programs is often a dealbreaker for small scripts and iteration in my experience

      Also, the REPL. Julia's REPL is vastly better than any other language REPL, by far. Python's is good but Julia is way better, even as a calculator for instance, it has fractions and it is more suited for maths

    • andriamanitra 50 minutes ago
      If you need to deal with matrices Julia's built-in support for that kind of stuff is the best out of any language I've ever seen (and I've tried dozens of different languages). It's like having first-class numpy arrays without installing any third party packages, and the syntax is even more convenient than Python. The standard library is reasonably comprehensive (not quite as big as in Python or Ruby or Go, but it's usually more well-designed).

      It is also an excellent language for messing about because the language and especially the REPL have tons of quality-of-life features. I often use it when I want to do something interactively (eg. inspect a data set, draw a graph, or figure out what's going on with an Unicode string, or debug some bitwise trickery).

      What Julia is not great at is things where you need minimal overhead. It is performant for serious number crunching like simulations or machine learning tasks, but the runtime is quite heavy for simple scripting and command-line tools (where the JIT doesn't really get a chance to kick in).

    • kryptiskt 1 hour ago
      Go is a total non-starter, it's not interactive at all. The competitors are things like Matlab, Mathematica, R or Python (with the right math libs). If you're weird you could use something like Haskell, APL or Lisp in this role, but you'd pay a hefty price in available libs.
    • xeonmc 7 minutes ago
      Its syntax is great for math and has a comprehensive ecosystem of scientific computing packages headed by the timelord guy from Men in Black 3.
    • Human-Cabbage 2 hours ago
      Julia is aimed at scientific computing. It competes against Python with numpy/scipy, R, etc.
      • ziotom78 2 hours ago
        Correct, but I would add: Julia is better than Python+NumPy/SciPy when you need extreme speed in custom logic that can’t be easily vectorized. As Julia is JIT-compiled, if your code calls most of the functions just once it won’t provide a big advantage, as the time spent compiling functions can be significant (e.g., if you use some library heavily based on macros).

        To produce plots out of data files, Python and R are probably the best solutions.

        • dgfl 1 hour ago
          Disagree on the last statement. Makie is tremendously superior to matplotlib. I love ggplot but it is slow, as all of R is. And my work isn’t so heavy on statistics anyway.

          Makie has the best API I’ve seen (mostly matlab / matplotlib inspired), the easiest layout engine, the best system for live interactive plots (Observables are amazing), and the best performance for large data and exploration. It’s just a phenomenal visualization library for anything I do. I suggest everyone to give it a try.

          Matlab is the only one that comes close, but it has its own pros and cons. I could write about the topic in detail, as I’ve spent a lot of time trying almost everything that exists across the major languages.

          • Certhas 1 hour ago
            I love Makie but for investigating our datasets Python is overall superior (I am not familiar enough with R), despite Julia having the superior Array Syntax and Makie having the better API. This is simply because of the brilliant library support available in scikit learn and the whole compilation overhead/TTFX issue. For these workflows it's a huge issue that restarting your interactive session takes minutes instead of seconds.
          • dan-robertson 1 hour ago
            I tried some Julia plotting libraries a few years ago and they had apis that were bad for interactively creating plots as well as often being buggy. I don’t have performance problems with ggplot so that’s what I tend to lean to. Matplotlib being bad isn’t much of a problem anymore as LLMs can translate from ggplot to matplotlib for you.
        • ainch 26 minutes ago
          Even then, if you're familiar with NumPy it's pretty easy to switch to Jax's NumPy API, and then you can easily jit in Python as well.
          • JanisErdmanis 3 minutes ago
            As long as someone else does the porting and maintains the compatability between both subecosystems of thoose who prefer using Jax and thoose who prefer depending on the NumPy. Also not having zero overhead structs that one can in an array handicaps types of performance codes one can write.
        • jey 1 hour ago
          And I would further add: In addition to performance, Julia's language and semantics are much more ergonomic and natural for mathematical and algorithmic code. Even linear algebra in Python is syntactically painful. (Yes, they added the "@" operator for matmul, but this is still true).
    • Certhas 1 hour ago
      On top of what others have said: In many situations the alternative to Julia isn't Go but C++ (or maybe Rust though its library support is lacking). E.g. if you are writing high-ish* performance algorithmic code that should also run on GPU. Julia also heavily prioritizes composability of libraries. And though that can be a source of bugs at times, there are things we are doing in Julia with one or two PhD students that would be practically impossible (as in require an order of magnitude more implementation work) in any other language.
    • wolvesechoes 1 hour ago
      It is highly interactive and dynamic, yet performant. And it is not only about scientific computing, for almost any application can take advantage of interactive, modifiable system, where you can explore your state at any point. In others, more static langs good debuggers help with this to lesser or larger extend, but it is not the same from my experience.

      So better question is: in which circumstances would you choose Julia over more mainstream-y alternative like Clojure? And here scientific and numerical angle comes to play.

      At the same time I think Julia is failed attempt, with unsolvable problems, but it is a different topic.

    • huijzer 1 hour ago
      To replace uses where you would use Matlab or R probably. I prefer Julia over Matlab or R. So data science. For production code however, it's not great since it has no static typing. Imagine having your production code crash mid-execution with the error "Function foo not found". Only dynamic languages can do that to you.
      • jondea 1 hour ago
        I broadly agree that it can be hard to nail down Julia's behaviour but it does have static typing and I think it is more subtle. Function arguments and variables can be concrete types e.g. if you were implementing an approximation for sin, you could restrict arguments to Float32 if you knew it was only suitably accurate for that type.
        • huijzer 27 minutes ago
          > it does have static typing and I think it is more subtle.

          Yes sure Julia isn't fully dynamically typed, but that doesn't change the fact that it isn't fully static typed. If it was, it should be pretty easy to create static binaries and find bugs like "func not defined" at compilation time.

    • setopt 2 hours ago
      Scientific computing. AFAIK, library support for that in Go is almost nonexistent.
    • markkitti 2 hours ago
      When I need to do serious math, I use Julia.
    • bandrami 1 hour ago
      Math. Places you might use Wolfram or Sage.
    • Joel_Mckay 59 minutes ago
      Julia collapses entire programming paradigms into single character syntax, and often will transparently handle clean parallelism or cluster instance batching.

      https://docs.julialang.org/en/v1/manual/mathematical-operati...

      Go is similar in many ways, but takes a performance hit in areas like Garbage collection.

      The Julia community is great, and performance projects may also be compiled into a binary image. =3