@Cedric: wow that's a cool trick I didn't know you could do that. @Yichao: Ok thanks, that makes sense.
On Tuesday, August 2, 2016 at 3:41:40 AM UTC-7, Yichao Yu wrote: > > > > On Tue, Aug 2, 2016 at 6:38 PM, Cedric St-Jean <[email protected] > <javascript:>> wrote: > >> I can't speak to Julia's branch prediction, but if you rewrite it as >> >> function f{x}(::Val{x}) ... >> ... >> end >> >> f(Val{True}) >> > > Don't do this. > > >> >> then the compiler will create two versions of f, one with the if and one >> without. Then you can @time the results and see if it makes a >> > > Which is generally bad except for synthetic benchmarks except for rare > cases. > > >> difference. >> >> >> On Tuesday, August 2, 2016 at 5:29:41 AM UTC-4, esproff wrote: >>> >>> So let's say I have a function of the form: >>> >>> function f(x::Bool) >>> for i in 1:10000000 >>> *(bunch of stuff)* >>> if x >>> do something >>> end >>> *(even more stuff)* >>> end >>> >> > Julia doesn't do "branch prediction", that's the job of the CPU. > A branch with a constant branch condition basically has no cost anyway. > You are not (syntactically) evaluating anything millions of times. > > >> >>> As you can see I only need to evaluate this if statement once, but >>> instead I'm (in theory) evaluating it millions of times. How good is >>> Julia's branch prediction? Does Julia's JIT compiler recognize this and >>> make it so the if statement is only evaluated once? >>> >> >
