Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
johnnychen94 opened this issue Aug 21, 2021 · 3 comments
Labels
best practice workflows and best practice that are useful in JuliaImages and/or Julia

Comments

@johnnychen94
Copy link
Member

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/

@johnnychen94 johnnychen94 added the best practice workflows and best practice that are useful in JuliaImages and/or Julia label Aug 21, 2021
@kimikage
Copy link

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?" 😝

@johnnychen94
Copy link
Member Author

johnnychen94 commented Aug 22, 2021

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
Copy link
Member

timholy commented Aug 22, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
best practice workflows and best practice that are useful in JuliaImages and/or Julia
Projects
None yet
Development

No branches or pull requests

3 participants