Skip to main content
16 events
when toggle format what by license comment
Feb 18, 2015 at 14:30 comment added John R. Strohm @delnan: Worst case, with the temporary, you fetch radius[i] once, store it once in the temp, and then fetch the temp a few times, AS OPPOSED to fetching radius[i] those few times. The question is how much extra does the subscript operation cost. Moreover, depending on what you are doing between uses of the (temp-cached) expression, the compiler may be required to assume that something could have changed, and do an expensive operation. Using a scope-limited temp tells him that the temp DIDN'T change.
Aug 13, 2014 at 17:06 comment added user7043 @JohnR.Strohm My point exactly, read again.
Aug 13, 2014 at 17:04 comment added John R. Strohm @randomA, the value may or may not reside in memory, depending on the target architecture, the compiler, the optimization level chosen, other code, and the phases of the moon, Mars, and Uranus. If the target machine has enough registers, and the compiler recognizes that there is no need to store the temporary in memory, it will just leave it in a register. NB: If you are on an x86 processor, the compiler essentially has no registers to play with, and everything must live in memory.
Aug 13, 2014 at 17:00 comment added John R. Strohm @delnan The compiler is required to obey the semantic roadblocks. The programmer may know that the roadblock does not apply in this case, but there is no way, other than explicitly assigning a temporary, for the programmer to convey that knowledge to the compiler.
Aug 13, 2014 at 14:47 answer added Mike Dunlavey timeline score: 0
Aug 7, 2014 at 21:42 comment added user22815 As @Useless mentions, profiling will tell you where to optimize. Even then, you are often better off leaving the slow parts alone. You may make them less readable/maintainable, for example. Unless the performance really is terrible or it is easy to fix without hurting the code's maintainability, don't bother.
Aug 7, 2014 at 13:43 comment added Useless It will all be optimized, if optimization is enabled. In general, this is controlled not by regions of code, but by types of optimization (although you can use different settings for different translation units). The bits that need to be optimized by hand are exactly the bits that prove to be too slow, when you profile.
Aug 7, 2014 at 12:29 answer added old_timer timeline score: 0
Aug 7, 2014 at 11:56 comment added S.E.K. Thanks all for your answers. How do I know the parts of the code that will be optimized by the compiler, and those that need to be optimized by hand, are there any rules to this?
Aug 7, 2014 at 11:44 answer added Useless timeline score: 4
Aug 7, 2014 at 11:27 comment added user7043 If you use radius[i] three times, the compiler is free (and will try) to load from memory only once and keep re-using it. Assuming of course there aren't any semantic roadblocks to that, like radius being volatile or intervening writes to potentially aliasing pointers. Local variables are mostly for reader comprehension, not for optimization.
Aug 7, 2014 at 11:19 comment added InformedA That is not correct, when you use variables in your C methods, the variables' values still reside in memory. The decision to use registers or not to hold the values of those variables are made by the compiler, not by high level language instruction
Aug 7, 2014 at 11:18 history edited Kilian Foth CC BY-SA 3.0
fix formatting
Aug 7, 2014 at 11:17 answer added Doval timeline score: 11
Aug 7, 2014 at 11:17 review First posts
Aug 7, 2014 at 12:56
Aug 7, 2014 at 11:06 history asked S.E.K. CC BY-SA 3.0