Yes. Omitting the argument types of functions does not sacrifice the performance.
Julia beginners tend to get bugs because they carelessly write the argument types of functions.
For example, when someone defines f(x::Array)
and executes f(a[:,1])
correctly, he is taught how to optimize using @view
, and he rewrites it as f(@view a[:,1])
, then his program will suddenly stop working.
The same can be said for f(x::String)
and SubString
.
When in doubt, omit the argument types.
Additional comment 1: The definition of struct
should be written in such a way that the concrete types of the fields are determined, otherwise performance degradation will occur (see Performance Tips · The Julia Language). However, even in that case, too much restriction on types can cause bugs. When in doubt, you can define struct
in the following way:
struct Foo{Ta, Tb, Tc}
a::Ta
b::Tb
c::Tc
end
Additional comment 2: If you check the compilation process and results with @code_typed
, @code_llvm
, @code_native
, etc., it will be more clear that the omission of argument types does not affect the performance.
Input:
f(x::Float64) = 2.0x + 1.0
@code_llvm debuginfo=:none f(1.2)
Output:
; Function Attrs: uwtable
define double @julia_f_1273(double %0) #0 {
top:
%1 = fmul double %0, 2.000000e+00
%2 = fadd double %1, 1.000000e+00
ret double %2
}
Input:
g(x) = 2x + 1
@code_llvm debuginfo=:none g(1.2)
Output:
; Function Attrs: uwtable
define double @julia_g_1275(double %0) #0 {
top:
%1 = fmul double %0, 2.000000e+00
%2 = fadd double %1, 1.000000e+00
ret double %2
}
The above means that the compilation results for executing f(1.2)
when f(x::Float64) = 2.0x
and for executing g(1.2)
when g(x) = 2x
are completely the same at the llvm level.
Note also that you don’t even need to write 2.0x + 1.0
because it is a floating point number calculation, 2x + 1
is enough. (See → Avoid using floats for numeric literals in generic code when possible)