4 ms·
As long as you write code that's type stable, yea. Type stable means that the compiler can deduce the types of the variables used in a function given the types
by jules 8y ago
As long as you write code that's type stable, yea. Type stable means that the compiler can deduce the types of the variables used in a function given the types of the arguments passed to the function. An example of code that's not type stable is code something like this:
function test1(n)
x = 0
for i = 1:n
if i == 10
x = x + 0.1
else
x = x + 1
end
end
return x
end
It isn't type stable because x starts out as an int but then changes to a float in the middle of the loop. This code compiles to 78 instructions.
The following code is type stable:
function test2(n)
x = 0.0
for i = 1:n
if i == 10
x = x + 0.1
else
x = x + 1.0
end
end
return x
end
This code compiles to 14 instructions.
- kristofferc 8y agoJulia is pretty good at dealing with this now though julia> @btime test1(10^5) 147.427 μs (0 allocations: 0 bytes) 99999.1 julia> @btime test2(10^5) 88.472 μs (0 allocations: 0 bytes) 99999.1 In earlier versions of Julia, the penalty here would be order of magnitudes worse.