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. 2 1600
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************* *********@hotma il.com
Remove only "_nos_pam"
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 tables in the database
where the code resides. If we move the database with the data tables to
a new directory, the links are no longer valid.
I tried to update the links by changing the Connect property and refreshing:
Set td = db.TableDefs(0)...
|
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( structure->bufferspace ) ).
My question is, if I free just the structure, will the (e.g.) bufferspace be
freed implicitly, or do I have to (as I currently am) free the members
first?
Thanks.
-cjl
|
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 for it to be
speedy for trees with thousands of nodes.
At its core, the structure is just a list of lists arranged so that if
node A has children B and C and node B has child D the data looks like:
A =
B =
|
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 conflicting views and
far from ideal solutions.
My application has to send small amounts of data about 50bytes to
multiple client applications. The catch is that this has to happen
about 1000 times a second.
My first attempt was .net remotting...
|
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 foo *next;
int a;
int b;
int c;
};
| |
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 members in a way similar to the old pascal
|
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 container, say another vector or list.
Code:
typedef struct
{
std::string name;
|
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) ();
void *arg;
struct threadpool_work *next;
} threadpool_work_t;
|
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, max_dep
SB *b
}
struct SB{
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |