What's a [good] way to make slow Julia code (with no side-effects)?


#1

[I was trying to prove to myself that left side of && is always run (unnecessarily, when right one hard-coded to return false).]

slow(n) = begin for i in 1:n end; return true end # Gets optimized away, Julia scores points.

[In theory, functions without side-effects, always returning same, should ALWAYS be dead-code eliminated; good for Julia if guaranteed.]

sleep(n) doesn’t work (for me), as it has side-effects.

julia> slow(n) = begin rand(n); return true end # This actually has side-effects.. could have used /dev/random ..

julia> if slow(1000000000) && false println("Dummy hello") end
ERROR: OutOfMemoryError()
 in rand at ./random.jl:300 [inlined]
 in rand at ./random.jl:297 [inlined]
 in rand at ./random.jl:229 [inlined]
 in rand at ./random.jl:230 [inlined]
 in slow(::Int64) at ./REPL[71]:1

julia> if sleep(1) && false println("Preferably wouldn't happen..; nor should it run sleep that doesn't return bool before finding out.") end
ERROR: TypeError: non-boolean (Void) used in boolean context

julia> fib(10) # I thought in Base..
ERROR: UndefVarError: fib not defined

julia> fibonacci(10)
ERROR: UndefVarError: fibonacci not defined


help?> ⊊ # just noticed, what is it, doc doesn't explain; diff from ⊈ that I know what is..

#2

Only expressions that are known to be effect-free can be optimized out on the left hand side of &&. Furthermore, the compiler currently doesn’t always do this optimization, even when effect-free is known (such as when a Base.@pure annotation exists.)

As for ⊊, this is mathematical notation for proper subset which is as you noted a distinct concept from “not a subset” (⊈). You are right that the difference does not seem to be documented.