On Fri, Jul 3, 2015 at 8:37 AM, Stefan Karpinski <[email protected]> wrote: > The first one creates a new array object every time it runs. It's a fairly > sophisticated analysis to prove that the array never escapes and is never > modified and can therefore be reused.
Also, in general, a global constant is faster than a local variable. > > On Fri, Jul 3, 2015 at 2:21 PM, Jeffrey Sarnoff <[email protected]> > wrote: >> >> I wrote a range limited isa_leapyear(year) good in 1800..2200. >> >> Your look is appreciated; I do not understand this: >> >> The first version runs 50x more slowly than the second version. >> The first is better practice, is there a way to make it behave? >> >> They differ only in placing the bit table (local vs const global). >> I ran both through code_typed(), the function code is the same. >> >> this is the way of the snail: >> >> function is_proximal_leapyear(year::Int) >> LeapYearBitTbl = >> [ 0xfdfffffe, 0xffffffff, 0xfffff7ff, 0x0000000f ] >> >> y400 = convert(Uint32, year-1800) >> y100 = y400 >> 2 >> mask = one(Uint32) << (y100 & 0x0000000f) >> indx = 1 + (y100 >> 5) >> ((LeapYearBitTbl[indx] $ mask) + (y400 & 0x00000003)) === zero(Uint32) >> end >> >> this way is lovely fast: >> >> const LeapYearBitTable = >> [ 0xfdfffffe, 0xffffffff, 0xfffff7ff, 0x0000000f ]; >> >> function isa_proximal_leapyear(year::Int) >> y400 = convert(Uint32, year-1800) >> y100 = y400 >> 2 >> mask = one(Uint32) << (y100 & 0x0000000f) >> indx = 1 + (y100 >> 5) >> ((LeapYearBitTable[indx] $ mask) + (y400 & 0x00000003)) === zero(Uint32) >> end >> >
