binary - Julia: Base function 10,000x quicker then similar function -
i'm playing around decimal binary converter 'bin()' in julia, wanting improve performance. need use bigints problem, , calling bin() bigint within file outputs correct binary representation; however, calling function similar bin() function costs minute in time, while bin() takes .003 seconds. why there huge difference?
function binbase(x::unsigned, pad::int, neg::bool) = neg + max(pad,sizeof(x)<<3-leading_zeros(x)) = array(uint8,i) while > neg a[i] = '0'+(x&0x1) x >>= 1 -= 1 end if neg; a[1]='-'; end asciistring(a) end function bin1(x::bigint, pad::int) y = bin(x) end function bin2(x::bigint, pad::int,a::array{uint8,1}, neg::bool) while pad > neg a[pad] = '0'+(x&0x1) x >>= 1 pad -= 1 end if neg; a[1]='-'; end asciistring(a) end function test() = array(uint8,1000001) x::bigint= 2 x = (x^1000000) @time bin1(x,1000001) @time bin2(x,1000001,a,true) end test()
as noted felipe lema, base delegates bigint printing gmp, can print bigints without doing intermediate computations them – doing lots of computations bigints figure out digits quite slow , ends allocating lot of memory. bottom line: doing x >>= 1
extremely efficient things int64 values not efficient things bigints.
Comments
Post a Comment