- Pointing to a variable slower than a variable? - 7 Updates
JiiPee <no@notvalid.com>: Sep 12 05:05PM +0100 Was coding and started to think about this: 1) struct A { int a; void change() { a = 5; } }; 2) struct X { int a; }; struct A { X x; void change() { x.a = 5; } }; Are 1) and 2) change()-calls (100%) equally fast? 2) is accessing a-variable by an object x; does it slow down (a bit) things? |
JiiPee <no@notvalid.com>: Sep 12 05:16PM +0100 struct X { int a; }; struct A { X x; void change() { x.a = 5; } }; okey, the reason am asking this is because I would like the A class not being able to modify directly the a-variable (which would be private in X). So I would rather put a to another class as a private member and create a function to modify it. is there another way to do the same thing; to hide the a from class A? anyway, because speed is important here (doing millions of calculations per second) i want to think about the speed as well. |
JiiPee <no@notvalid.com>: Sep 12 05:29PM +0100 2) struct X { int a; }; struct A { X x; void use_a_alot() }; Also, if x.a is slower than having a-member in A, is it faster to copy the x.a to a temporary variable if using it a lot... like this: void A::use_a_alot() { int temp = x.a; //... and then use temp like 10 times in the code // ,....using temp... } or is as fast to use x.a? |
"Öö Tiib" <ootiib@hot.ee>: Sep 12 09:39AM -0700 On Saturday, 12 September 2015 19:05:49 UTC+3, JiiPee wrote: > }; > Are 1) and 2) change()-calls (100%) equally fast? 2) is accessing > a-variable by an object x; does it slow down (a bit) things? It is question about quality of implementation. C++ standard does not have requirements about it. By my experience with real C++ compilers there can be difference when the code is compiled for debugging but there are no difference when the code is compiled and optimized for releasing as application, so struct that contains one int is processed as fast at int itself. Note that the difference is rather important. Always profile the release versions since the debug versions can be orders of magnitude slower. |
"Öö Tiib" <ootiib@hot.ee>: Sep 12 09:57AM -0700 On Saturday, 12 September 2015 19:29:40 UTC+3, JiiPee wrote: > // ,....using temp... > } > or is as fast to use x.a? In practice also array of single element is as quick as just element so following code is as fast as usage of bare 'int a' member of 'A' in practice: 3) struct X { int a[1]; }; struct A { X x[1]; void use_a0_of_x0_alot(); }; However nothing of it is guaranteed by standard. |
Richard Damon <Richard@Damon-Family.org>: Sep 12 03:01PM -0400 On 9/12/15 12:16 PM, JiiPee wrote: > is there another way to do the same thing; to hide the a from class A? > anyway, because speed is important here (doing millions of calculations > per second) i want to think about the speed as well. If you look at the assembly code that would typically be generated from the two expressions ( a being a member of A or a being a member of X being a member of A ) they would be the same, as a is just a chunk of memory at a fixed offset to the base address of the object. Now, if you change a to be private in X, then A can't write x.a = 5, so you will need a function in X to do the operation. Maybe something like: struct X { void change() { a = 5; } private: int a; } struct A { X x; void change() { x.change(); } } Now, but the description of the abstract machine, we have some additional operations, for example, A:change() needs to compute the address of x for instance, to pass to X:change(). Since everything is inline, it is likely that this will still get converted (at least when optimization is enabled) to the same code as before. |
JiiPee <no@notvalid.com>: Sep 12 08:27PM +0100 On 12/09/2015 20:01, Richard Damon wrote: > X x; > void change() { x.change(); } > } yes > address of x for instance, to pass to X:change(). Since everything is > inline, it is likely that this will still get converted (at least when > optimization is enabled) to the same code as before. yes, this is the situation. so you would think in release they have the same speed. Ok, I guess I can do the private thing.... its much better structure as well. but i guess I can measure the time to be sure about it. |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment