How often is type declaration necessary in Julia for optimal performance? -
i have started translating python game engine boardgame have invented. know example going show pretty meaningless, there no real need optimize it, right before dealing heavy functions.
function howmany()::int8 pieces::int8 = 0 while pieces > 8 || pieces < 4 try print("how many pieces going play (min 4, max 8)? ") pieces = parse(int8, readline(stdin)) catch println("it must integer number between 4 , 8!") end end return pieces end function main() pieces::int8 = howmany() #println(pieces, typeof(pieces)) end main()
is necessary declare int8 4 times (3 declaration + parse parameter)? when can avoid specifying int8 without having performance trade off?
twice in following avoids expensive try-catch
:
function howmany() while true print("how many pieces going play (min 4, max 8)? ") pieces = get(tryparse(int8, readline(stdin)), int8(0)) 4 <= pieces <= 8 && return pieces println("it must integer number between 4 , 8!") end end function main() pieces = howmany() println(pieces, typeof(pieces)) end main()
uses nice short-cuts such as:
get
defaultshort-circuit
&&
instead of bulkierif ... end
.
and code-stable @code_warntype howmany()
shows.
Comments
Post a Comment