Is it always true that self-defined types/structs are quicker than using packages written by others? which do you prefer

Hi, as the topic title goes, I hope this question is not naive. So what do you think then?

No. Where did you get the idea?

3 Likes

Julia has no special languages or semantics with cannot be used. Plus everything is open source in Julia.
Thus you cannot in general do better or there is no limitation why you need to do worse than any packages even Julia base.

That said, it is true that if a package caters for a more generalised version of a function whereas you can get by with a more simplified case due to the constraints of your problem, a custom one might theoretically be able to do better. However remember that Julia only compiles the functions that you need and for the datatypes you use, thus in many cases the more generalised functionality that you don’t need would just not even be compiled in (that depends on how the code is structured).

Also there is a bit difference between a novice writing code and an experienced Julia coder. More than likely for the first while of using Julia, your code will be much slower than it can theoretically be.
Thus even if you can save some by not using the generalised version, the losses due to the novice Julia inexperience inefficiencies would likely cancel out any potential gains.

Granted not all packages are made equal and some concentrate on the functionality (my Unmarshal.jl package for example, I haven’t really tried to go back and optimise thus I wouldn’t be surprised if you can create a version that supports unmarshaling your specific usecase much more efficiently), whereas some of the packages like Images.jl and the like are extremely well optimised and unless you can both understand fully what they did in that code to get the performance and why they made such choices and are able to synthesise new code using similar approaches, you are unlikely to be doing better an likely will be wasting your time creating a custom version.

But you specifically mentioning types/structs more than functionality. Do you have specific examples of what you are thinking about?

2 Likes

It depends on what my goal is and what my expertise is. For example, I don’t use MacroTools at all because I prefer to write my own custom code for AST transformations. In general, I like to come up with my own abstractions which suit my needs. Originally I had my own benchmark macro also, but now I use BenchmarkTools. For large and complex tools, it makes sense to try out existing code before considering making your own… if you are creative and have better ideas for starting anew. It’s a balance between what is convenient, necessary, efficient, useful, etc. There is no single rule which can determine whether to make custom code or to use an existing tool which applies to every scenario, since everyone’s expertise, goals, resources, etc are different.

If you need to make something compatible or reproducible or dependable, it might make sense to prioritize the usage of common tools.