JuliaImages / juliaimages.github.io

Documentation For JuliaImages
https://juliaimages.org
33 stars 56 forks source link

Don't spend your time on vectorizing your codes just to make things slower (CPU) #220

Open johnnychen94 opened 3 years ago

johnnychen94 commented 3 years ago

People in Python and Matlab communities enjoy "vectorizing my image processing pipeline and getting a big performance boost" story. The only reason for that is because for-loop is slow in Matlab/Python, and vectorizing the pipeline is indeed a tradeoff between "calling fast C/Fortran backend" and "overhead in intermediate memory allocation".

In Julia, unless you're writing GPU codes, use simple for-loop should be easier to write, and usually get you 2x-3x performance boost than vectorized codes.

And a lot of functions or helper utils (e.g., im2col/col2im #219) becomes useless in Julia because we don't need to twist our mind to vectorize the codes to get high performance.

Reference: https://julialang.org/blog/2017/01/moredots/

kimikage commented 3 years ago

This title seems like clickbait. I think the term "vectorization" needs to be used with caution. As you know, its usage in the Python community, i.e. using (NumPy) arrays instead of for-loops, is not so common in other languages (e.g. Julia).

The important thing to remember is that "premature optimization is the root of all evil".

A "best" practice for getting speed is to paste the target code into the Discourse with the title "Why is Julia slower than Python?" :stuck_out_tongue_closed_eyes:

johnnychen94 commented 3 years ago

I'm trying to collect some ideas for a series juliaimages blog posts or tutorials to explain some of the core ideas to write "native" julia codes in a way that JuliaImages developers do. I open this as I believe this is something very important for users who are doing image processing in dynamic languages like Matlab/Python.

This actually isn't about premature optimization; in Matlab/Python a lot of people faithfully believe the power of vectorization when optimizing the codes. Without explaining why it doesn't work in Julia, they would otherwise try to invent all the wheels like im2col, meshgrid just to follow the Matlab/Python best practice that no longer holds in Julia.

https://julialang.org/blog/2017/01/moredots/ is a good explanation on this topic, but I'm thinking of a more concrete image processing example in real-world so that it attracts our users' attention.

timholy commented 3 years ago

I agree that having a couple of short image-processing examples, coupled with a reference to a more detailed yet more general discussion elsewhere, would make a lot of sense.