performance - Variable storage versus redundant arithmetic -
i'm writing simple loop in lua lÖve game i'm developing. understand i'll waste more time worrying ever spent on cpu clock cycles answer question saves me, want deeper knowledge of how works.
the current body of loop so:
local low = mid - diff local high = mid + diff love.graphics.line(low, 0, low, wheight) love.graphics.line(high, 0, high, wheight)
i want know if more computationally efficient keep or change to:
love.graphics.line(mid - diff, 0, mid - diff, wheight) love.graphics.line(mid + diff, 0, mid + diff, wheight)
with second body, have calculate low , high differences twice each. first, have store them in memory , access them twice each.
which more efficient?
the short answer it'll unlikely make difference @ all. if there kind of difference, code next drawing line, example. drawing aliased line optimized bresenham implemented in native code enormously expensive in comparison add , subtract. function call alone dwarf cost.
with second body, have calculate low , high differences twice each. first, have store them in memory , access them twice each.
this not case. variables don't "store memory" in ways expressions don't. can directly map register. likewise, avoiding variables doesn't "avoid memory". expressions likewise computed , stored in registers, whether explicitly assign intermediate results variables or not.
so memory standpoint, both versions of code need use registers store intermediate results of computation.
memoization doesn't have kind of memory overhead when you're involving simple variables because types map directly registers without stack spills. when start computing whole arrays/tables in advance, doing additional computation faster memoization if memoization means more dram access (in case memory overhead can outweigh savings). simple pod-type variables numbers don't have dram overhead, map directly registers. in other words, they're literally free: compiler emit same machine code whether or not assigned result of expressions local variables or not -- same number of registers required.
local variables data types map directly gp registers best thought existing while you're in high-level coding land. time jit or interpreter compiles code form machine understands, they'll disappear , turn registers regardless of whether created variables or not.
probably ultimate question, if there difference, whether redundant computation can eliminated. take trivial optimizer figure out mid - diff
written twice in exact same statement needs computed once. i'd surprised if didn't optimized away time reaches ir instruction selection , register allocation stage.
but if turned out surprise, , lua interpreter inefficient fail recognize redundant computation , performed anyway, again, have code next renders line (which involves loopy rasterization). relatively speaking, practically free redundancy. here it's not worth sweating such small stuff, , coming obsessed shaving clock cycles.
Comments
Post a Comment