473,789 Members | 2,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optimization with local vs. global arrays

The execution speed of the following code is dramatically faster if I
declare some arrays globally rather than locally. That is

FOO a[10], b[10], c[10];

void bar() {
...
}

runs much faster (up to 33%) than

void bar() {
FOO a[10], b[10], c[10];
...
}

There is considerable work being performed in the ... section.
This is on a Linux Itanium II system, compiled both with the Intel C++
compiler (V9.1) with interprocedural optimization enabled, and with the
GNU C V 3.3.5 compiler with -O3 optimization. (The performance change is
more dramatic with the Intel Compiler.) I tried declaring the local
FOO arrays static with

static FOO a[10], b[10], c[10];

which helped with the GNU compiler but was actually worse with the Intel
compiler. I also tried

FOO d[30];
FOO *a = d, *b = d+10, *c = d+20;

with a local d array, but that had no effect.

Is this just a compiler issue, or am I missing something? I want to avoid
the external arrays, obviously, but that code compiled by the Intel
compiler gives the fastest execution speed by far. I'd like to get the
equivalent performance with something less dangerous than global arrays.
Apr 12 '07
19 2464
Jim West wrote:
The execution speed of the following code is dramatically faster if I
declare some arrays globally rather than locally. That is

FOO a[10], b[10], c[10];

void bar() {
...
}

runs much faster (up to 33%) than

void bar() {
FOO a[10], b[10], c[10];
...
}
If, as seems to be the case, the culprit is the constructor for FOO, you
can just remove it. Since the version with the global array apparently
works correctly, the rest of the code isn't relying on having the FOO
values initialized on entry into bar.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Apr 12 '07 #11

"Pete Becker" <pe**@versatile coding.comwrote in message
news:Qa******** *************** *******@giganew s.com...
Jim West wrote:
>The execution speed of the following code is dramatically faster if I
declare some arrays globally rather than locally. That is

FOO a[10], b[10], c[10];

void bar() {
...
}

runs much faster (up to 33%) than

void bar() {
FOO a[10], b[10], c[10];
...
}

If, as seems to be the case, the culprit is the constructor for FOO, you
can just remove it. Since the version with the global array apparently
works correctly, the rest of the code isn't relying on having the FOO
values initialized on entry into bar.
In another part of the thread, he shows two constructors, one default and
one parameterized. He can't remove the default constructor because the
array definitions require one, and the compiler can't create one if there's
a parameterized version, right? (Of course, he may be able to remove the
parameterized version as well; I don't know.) But won't the
compiler-generated constructor perform the same default initializations , and
still get called 30 times for each call to bar? Or am I misunderstandin g
something here?

In any case, what I might do in his case is declare the arrays in the
function which calls bar, prior to the loop that [apparently] calls bar
repeatedly, and simply pass pointers to those arrays to bar.

-Howard


Apr 12 '07 #12
In article <hP************ *********@bgtns c04-news.ops.worldn et.att.net>,
Howard <al*****@hotmai l.comwrote:
>"Pete Becker" <pe**@versatile coding.comwrote in message
news:Qa******* *************** ********@gigane ws.com...
>Jim West wrote:
>>The execution speed of the following code is dramatically faster if I
declare some arrays globally rather than locally. That is

FOO a[10], b[10], c[10];

void bar() {
...
}

runs much faster (up to 33%) than

void bar() {
FOO a[10], b[10], c[10];
...
}

If, as seems to be the case, the culprit is the constructor for FOO, you
can just remove it. Since the version with the global array apparently
works correctly, the rest of the code isn't relying on having the FOO
values initialized on entry into bar.

In another part of the thread, he shows two constructors, one default and
one parameterized. He can't remove the default constructor because the
array definitions require one, and the compiler can't create one if there's
a parameterized version, right? (Of course, he may be able to remove the
parameterize d version as well; I don't know.) But won't the
compiler-generated constructor perform the same default initializations , and
still get called 30 times for each call to bar? Or am I misunderstandin g
something here?

In any case, what I might do in his case is declare the arrays in the
function which calls bar, prior to the loop that [apparently] calls bar
repeatedly, and simply pass pointers to those arrays to bar.
That was added later in the thread. Pete's point was probably
that (even in light of any new info) that since he is worried
about speed that the global array's (even if tossed into a named
or named namespace) are zero-initialized first because they are static
before other intialization on them occurs.

BTW, this can be different from having static's inside the function.
Actually, a bit of this, including the timing of the zero'ing of the
gloabals, is up to the compiler [system].

Lastly to Jim, if this is utterly crucial, there is probably
some other solution possible even faster than the globals,
but we probably don't know enough about what you're doing
to offer that at this point.
--
Greg Comeau / 4.3.9 with C++0xisms now in beta!
Comeau C/C++ ONLINE == http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Apr 12 '07 #13
Howard wrote:
"Pete Becker" <pe**@versatile coding.comwrote in message
news:Qa******** *************** *******@giganew s.com...
>Jim West wrote:
>>The execution speed of the following code is dramatically faster if I
declare some arrays globally rather than locally. That is

FOO a[10], b[10], c[10];

void bar() {
...
}

runs much faster (up to 33%) than

void bar() {
FOO a[10], b[10], c[10];
...
}
If, as seems to be the case, the culprit is the constructor for FOO, you
can just remove it. Since the version with the global array apparently
works correctly, the rest of the code isn't relying on having the FOO
values initialized on entry into bar.

In another part of the thread, he shows two constructors, one default and
one parameterized. He can't remove the default constructor because the
array definitions require one, and the compiler can't create one if there's
a parameterized version, right?
Shrug. Write an empty default constructor, or refactor. I'm not
particularly interested in getting into implementation details. The
point is that the initialization apparently isn't actually required, so
doesn't belong in the class.

(Of course, he may be able to remove the
parameterized version as well; I don't know.) But won't the
compiler-generated constructor perform the same default initializations , and
still get called 30 times for each call to bar? Or am I misunderstandin g
something here?
The compiler-generated constructor uses the default initializer for each
of the float fields, and that initializer does nothing. Any reasonable
compiler will generate no code.
>
In any case, what I might do in his case is declare the arrays in the
function which calls bar, prior to the loop that [apparently] calls bar
repeatedly, and simply pass pointers to those arrays to bar.
But that unnecessarily increases coupling because every caller now has
to provide scratch data for bar.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Apr 12 '07 #14
Greg Comeau wrote:
>
That was added later in the thread. Pete's point was probably
that (even in light of any new info) that since he is worried
about speed that the global array's (even if tossed into a named
or named namespace) are zero-initialized first because they are static
before other intialization on them occurs.
Not quite. My point was that the global arrays demonstrate that
initialization is irrelevant, because on calls to bar after the first
one they have whatever junk was left over from the previous call. If
that works, then skip the initialization entirely.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Apr 12 '07 #15

"Pete Becker" <pe**@versatile coding.comwrote in message
news:6Z******** *************** *******@giganew s.com...
>>
In any case, what I might do in his case is declare the arrays in the
function which calls bar, prior to the loop that [apparently] calls bar
repeatedly, and simply pass pointers to those arrays to bar.

But that unnecessarily increases coupling because every caller now has to
provide scratch data for bar.
True. I hadn't thought of that. I guess without knowing more details of
how bar is called and what it's used for, it's tough to come up with the
"best" solution.

-Howard

Apr 12 '07 #16
On 2007-04-12, Howard <al*****@hotmai l.comwrote:
>
"Pete Becker" <pe**@versatile coding.comwrote in message
news:6Z******** *************** *******@giganew s.com...
>>>
In any case, what I might do in his case is declare the arrays in the
function which calls bar, prior to the loop that [apparently] calls bar
repeatedly, and simply pass pointers to those arrays to bar.

But that unnecessarily increases coupling because every caller now has to
provide scratch data for bar.

True. I hadn't thought of that. I guess without knowing more details of
how bar is called and what it's used for, it's tough to come up with the
"best" solution.
Following up on my situation...I unfortunately can't post to newsgroups
at work, but I followed the discussions on google groups. While the FOO
class need not be initialized in this particular case, it is part of a
numerical library that my group has put together over the years, and any
changes to the default constructor would potentially break a lot of
code. Initializing all components to zero seemed like a Good Idea(tm)
when I first wrote it a long time ago, but in hindsight it was clearly a
mistake. In any event, I copied the header to this particular file and
changed FOO to FOO2 everywhere and changed the default constructor to

FOO2() { };

This did not give any improvement, surprisingly. (At least with the
Intel compiler...I forgot to try g++.)

Allocating the arrays in the calling routines would work, but bar() is
indeed called in several different places and would require more editing
than I want to do at this point.

So, my solution is to use the namespace idea that Mark P (I believe)
suggested (already implemented). I need to protect them because their
real names are

FOO Observation_Poi nts[10], Source_Points[10], Basis_Functions[10];

names which get reused in a lot of different places throughout the code.
If I get in the habit of declaring things globally but not protected by
namespaces I'm afraid it would only be a matter of time before bar7()
calls bar8() and changes values unexpectedly.

Finally, I think that this change is going to be sufficient. This is a
computational electromagnetic s code, and the complex math involved
within bar() is quite intensive (it performs a four-dimensional numerical
quadrature to yield an electromagnetic field), and should overwhelm
any more minor tweaking that is possible. (Famous last words...I thought
the same about the constructor!)

Thanks for the help everyone. I really appreciate it, and I have learned
quite a bit about the cost of constructors that seem really, really
simple on the surface!

- Jim
Apr 13 '07 #17
On 2007-04-13, Jim West <eg***********@ yahoo.comwrote:
code. Initializing all components to zero seemed like a Good Idea(tm)
when I first wrote it a long time ago, but in hindsight it was clearly a
mistake. In any event, I copied the header to this particular file and
changed FOO to FOO2 everywhere and changed the default constructor to

FOO2() { };

This did not give any improvement, surprisingly.
I take that back. I mis-read 33.8 seconds as 38 seconds (I must be
really tired). 33.8 is only slightly slower than I get with the
global arrays. I'll have to re-consider changing the class default
constructor and see how many things really do break.

I think I'll go to bed now before I hurt myself.
Apr 13 '07 #18
Jim West wrote:
On 2007-04-13, Jim West <eg***********@ yahoo.comwrote:
>code. Initializing all components to zero seemed like a Good Idea(tm)
when I first wrote it a long time ago, but in hindsight it was clearly a
mistake. In any event, I copied the header to this particular file and
changed FOO to FOO2 everywhere and changed the default constructor to

FOO2() { };

This did not give any improvement, surprisingly.

I take that back. I mis-read 33.8 seconds as 38 seconds (I must be
really tired). 33.8 is only slightly slower than I get with the
global arrays. I'll have to re-consider changing the class default
constructor and see how many things really do break.

I think I'll go to bed now before I hurt myself.
Benchmarking sure is fun!

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
Apr 13 '07 #19
Jim West wrote:
On 2007-04-13, Jim West <eg***********@ yahoo.comwrote:
>>code. Initializing all components to zero seemed like a Good Idea(tm)
when I first wrote it a long time ago, but in hindsight it was clearly a
mistake. In any event, I copied the header to this particular file and
changed FOO to FOO2 everywhere and changed the default constructor to

FOO2() { };

This did not give any improvement, surprisingly.


I take that back. I mis-read 33.8 seconds as 38 seconds (I must be
really tired). 33.8 is only slightly slower than I get with the
global arrays. I'll have to re-consider changing the class default
constructor and see how many things really do break.
I suggest you investigate profilers for your platform/tools, I'm
guessing your platform is Linux, if so, Sun Studio has some excellent
profiling and analysis tools you could use.

--
Ian Collins.
Apr 13 '07 #20

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

Similar topics

33
3052
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
7
2693
by: Rajeev | last post by:
Hello, I'm using gcc 3.4.2 on a Xeon (P4) platform, all kinds of speed optimizations turned on. For the following loop R=(evaluate here); // float N=(evaluate here); // N min=1 max=100 median=66 for (i=0;i<N;i++){ R+=A*B*K; // all variables are float=4 bytes
15
1643
by: MackS | last post by:
The system I am working on supports a subset of C99, among which "standard-compliant VLAs". I've already learnt that VLAs can't have global scope. My question is whether I can safely declare a (local) VLA to have as its dimension a global variable of type int: short dim; int main(void)
12
2403
by: rodneys | last post by:
Hi, please take a look to this sample code: class MyClass { private: static int length ; public: static void setLength(int newLength) ; void do() ;
5
2394
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
206
8376
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
18
2106
by: terminator(jam) | last post by:
consider: struct memory_pig{//a really large type: memory_pig(){ std::cout<<"mem pig default\n"; //etc... }; memory_pig(memory_pig const&){
7
1150
by: =?GB2312?B?zPC5zw==?= | last post by:
Howdy, I wonder whether python compiler does basic optimizations to .py. Eg: t = self.a.b t.c = ... t.d = ... ..vs. self.a.b.c = ... self.a.b.d = ... which one is more effective? Since each dot invokes a hash table lookup, it
4
2624
by: raylopez99 | last post by:
Why is the same variable local inside a 'foreach' loop yet 'global' in scope (or to the class) outside it? RL class MyClass { int MyMemberArray1; //member variables, arrays, that are "global" to the class
0
9666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
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,...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
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,...
0
9020
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, 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...
0
6769
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();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4093
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 we have to send another system
3
2909
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 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...

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.