5 ms·
What would be your opinion of this comment, though? // Reset the counter for the next run total_count = 0;
by jbay808 5y ago
What would be your opinion of this comment, though?
// Reset the counter for the next run
total_count = 0;
- latchkey 5y agowhy are you resetting the counter?
- dlp211 5y agoFor the next run. Why is in the comment, it's likely a design flaw though and no comment is going to help here. See comment about using proper scoping.
- rrose 5y agoit feels like this suggests a design flaw. You should probably be creating a new local `total_count` variable for each run that goes out of scope at the end of the run, rather than reusing the same variable over and over again in different contexts
- wongarsu 5y ago// reuse counter for performance, this brings 2% speedup total_count = 0; would be a quite useful why comment, if not creating a new variable each time is intentional
- rrose 5y agosure, but reusing an integer variable is never going to bring a 2% speedup. allocating a new variable of almost any type is not going to cause any performance issues, at least if it is allocated on the stack, and if the construction of the variable is particularly expensive, resetting it probably will be too. I would need to see a realistic example to believe that something like this would ever be a useful comment.
- _old_dude_ 5y agoIn a compiled language, this is true for a local variable in general, it costs nothing because the compiler uses a register for it (or at worst spill it on stack but the stack frame size is reserved once for all stack allocated variables when the function is called). The story is different, if the variable is captured by a closure/lambda, the current stack frame or part of it may be allocated on heap, leading to perf issues.
- slavik81 5y ago> the stack frame size is reserved once for all stack allocated variables when the function is called Is there somewhere that's documented for LLVM? That's my expectation of what the final compiler output should be, but I read that the IR allocates every variable dynamically, and then they optimize away whatever they can. I haven't been able to figure out how or where it's guaranteed that all allocations will be coalesced. I wouldn't normally worry about it, but I've talked to folks who don't believe me when I say it doesn't matter whether you declare an integer inside or outside of a loop. It would be nice to be able to explain exactly why it can never matter.
- _old_dude_ 5y agoI don't know LLVM well enough, but you can play with godbolt By example, with https://godbolt.org/z/9Kv7oo9oK https://godbolt.org/z/9Kv7oo9oK you can see that values goes into registers (and that thank to 'lea' there is not many registers used). And if you remove the option -O2, values are spilled on stack.
- hnlmorg 5y agoI feel like you're getting bogged down on the literal code and missing the point of a hypothetical example.
- Normal_gaussian 5y agoIgnoring the pedantry elsewhere, then sure this comment explains what you are doing. The contention is whether it is necessary or common enough to be assumed. In most places this would be self evident and should be avoided to reduce mental load, however I have seen and done exactly this many times - in distrubuted or embedded systems where the control is neither synchronous or co-located (ie, coming in from interrupts / messages). In such places there may even be call for more description, however presuming the context is appropriate this could be correct. I would imagine a game with a react redux style state engine might result in a comment like this as well.