473,499 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fast access to members of structure

Hello everybody.

I've got such an aching me question concerning fast & well optimized
access to members of a structure (or class) and ways that VC compiler
handles it. Let's imagine the following structure and an array of its
type:

struct SSth1
{
int nMbr1, nMbr2, nMbr3;
} aSth1[1000];

now, when I access members within a loop, I can do the following:

for (int i=0; i<1000; i++)
{
for (int j=0; j<1000; j++)
{
doSth(j, &aSth1[i].nMbr1);
doSth(j, &aSth1[i].nMbr2);
doSth(j, &aSth1[i].nMbr3);
}
}

The doSth is just a method modifying the given above variables. Now, I
can also do the following:

for (int i=0; i<1000; i++)
{
int* nMbrLoc1=&aSth1[i].nMbr1;
int* nMbrLoc2=&aSth1[i].nMbr2;
int* nMbrLoc3=&aSth1[i].nMbr3;

for (int j=0; j<1000; j++)
{
doSth(j, nMbrLoc1);
doSth(j, nMbrLoc2);
doSth(j, nMbrLoc3);
}
}

I reckon that the second piece of code will be performed faster since
during execution of the code the pointers to the aSth1[i].nMbr1 will be
evaluated 1000 times rather than 1000*1000 times. Is that right? This is
what I assume. Alas, I do not know, how actually VC 2005 compiler
handles optimization issues like above. Can anyone elaborate a bit on
that? Do I have to follow that second way of coding, or, by any chance
compiler could handle pointer evaluation robustly on itself?
Cheers,
Peter.

Apr 17 '07 #1
2 1589
I reckon that the second piece of code will be performed faster since
during execution of the code the pointers to the aSth1[i].nMbr1 will be
evaluated 1000 times rather than 1000*1000 times. Is that right? This is
what I assume. Alas, I do not know, how actually VC 2005 compiler
handles optimization issues like above. Can anyone elaborate a bit on
that? Do I have to follow that second way of coding, or, by any chance
compiler could handle pointer evaluation robustly on itself?
There are 2 correct answers:
1) check the asm
2) test

Really, you can discus this until your hairs are grey but the only sensible
things is to measure it, and use real life scenarios.
It is easy enough to contrive examples where some source code optimization
can make a difference, but real-life performance is what matters.

The VC optimizer is pretty good, and when you use profile guided
optimization, it will do things that are beyond any source level optimization.

Without correct measurements, it is impossible to do anything.

--
Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Apr 17 '07 #2
A n g l e r wrote:
Hello everybody.

I've got such an aching me question concerning fast & well optimized
access to members of a structure (or class) and ways that VC compiler
handles it. Let's imagine the following structure and an array of its type:

struct SSth1
{
int nMbr1, nMbr2, nMbr3;
} aSth1[1000];

now, when I access members within a loop, I can do the following:

for (int i=0; i<1000; i++)
{
for (int j=0; j<1000; j++)
{
doSth(j, &aSth1[i].nMbr1);
doSth(j, &aSth1[i].nMbr2);
doSth(j, &aSth1[i].nMbr3);
}
}

The doSth is just a method modifying the given above variables. Now, I
can also do the following:

for (int i=0; i<1000; i++)
{
int* nMbrLoc1=&aSth1[i].nMbr1;
int* nMbrLoc2=&aSth1[i].nMbr2;
int* nMbrLoc3=&aSth1[i].nMbr3;

for (int j=0; j<1000; j++)
{
doSth(j, nMbrLoc1);
doSth(j, nMbrLoc2);
doSth(j, nMbrLoc3);
}
}

I reckon that the second piece of code will be performed faster since
during execution of the code the pointers to the aSth1[i].nMbr1 will be
evaluated 1000 times rather than 1000*1000 times. Is that right? This is
what I assume. Alas, I do not know, how actually VC 2005 compiler
handles optimization issues like above. Can anyone elaborate a bit on
that? Do I have to follow that second way of coding, or, by any chance
compiler could handle pointer evaluation robustly on itself?
Further to Bruno's comments, does it matter which is faster? Generally,
you should program for efficiency by using well designed algorithms, and
then write the code for these algorithms in the clearest way possible
(obviously sticking to efficient idioms where it doesn't impact
clarity). Only if you find you have bottlenecks for particular bits of
code should you consider 'micro-optimization'. In this particular case,
I imagine that the compiler optimizes it fine, but the only way to be
sure it to compile it with the optimizations you intend to use, and
check the execution time and asm output. But the point is that it would
usually be a wasted exercise, unless that bit of code is a bottleneck.
In the code above, a more likely bottleneck is the implementation of doSth.

Tom
Apr 17 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
23992
by: Random Person | last post by:
Does anyone know how to use VBA to relink tables between two MS Access databases? We have two databases, one with VBA code and the other with data tables. The tables are referenced by linked...
13
7264
by: John | last post by:
In the course of an assignment, I learned the hard way that I shouldn't try to free a malloc'd member of a malloc'd structure after having freed that structure (i.e., free( structure ); free(...
1
2723
by: David Hirschfield | last post by:
I've written a tree-like data structure that stores arbitrary python objects. The objective was for the tree structure to allow any number of children per node, and any number of root nodes...and...
11
4316
by: Olie | last post by:
This post is realy to get some opinions on the best way of getting fast comunication between multiple applications. I have scowered the web for imformation on this subject and have just found...
6
5908
by: Urs Thuermann | last post by:
With offsetof() I can get the offset of a member in a struct. AFAICS, it is portable and clean to use this offset to access that member. I need to do something like this struct foo { struct...
6
2998
by: noone | last post by:
What is the syntax to access members of a structure without explicitly naming the structure in every access? struct mytype { int a; char* b; long c; } IT; How can I access the structure...
2
7019
by: DhaneshNair | last post by:
I have created a COM server in which a collection of vectors need to be exposed to C# sharp client . Each vector contains a array of structures and this vector need to be embedded inside a...
5
2658
by: Mahendra Kumar Kutare | last post by:
I am trying to implement a webserver with boss-worker model thread pool implementation - I have a header declaration threadpool.h as - typedef struct threadpool_work { void (*routine) ();...
0
953
by: abarun22 | last post by:
Hi I would like to know if there are any ways to access the members of a nested structure inside python. I use SWIG as interface for C. My structure comes as follows. struct SA{ int nb_dep,...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7012
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.