473,779 Members | 2,015 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 #1
19 2460

"Jim West" <eg***********@ yahoo.comwrote in message
news:9F******** ***********@new sfe20.lga...
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.
Faster processor?
Apr 12 '07 #2
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];
...
}

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.
It's not especially surprising that the local arrays, which may be
pushed on the stack with each invocation of bar, would be slower than
the global arrays. If you want something "safer" you could try moving
the arrays to a namespace.

Mark
Apr 12 '07 #3
On 2007-04-12, GeekBoy <ne*@nerdy.comw rote:
>
Faster processor?
No, all are run on the same system, OS etc. It is compiled with
the Intel compiler using

icc -O3 -ip -c foo.cc

and with the GNU compiler using

g++ -O3 -c foo.cc
Apr 12 '07 #4
On 2007-04-12, Mark P <us****@fall200 5REMOVE.fastmai lCAPS.fmwrote:
It's not especially surprising that the local arrays, which may be
pushed on the stack with each invocation of bar, would be slower than
the global arrays. If you want something "safer" you could try moving
the arrays to a namespace.
OK, I had thought that the time needed to push the small arrays on the
stack (FOO isn't a very large class) would be small compared to the
heavy number crunching I do in the bar() routine. Guess not!

The namespace solution is what I needed, since some of the array names
are reused through-out the code. Seems obvious once it was pointed out.
:)

Thanks for the help.
Apr 12 '07 #5
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];
...
}
What is a FOO?

Does it require construction?

Do you call bar() in a loop?

--
Ian Collins.
Apr 12 '07 #6
On 2007-04-12, Ian Collins <ia******@hotma il.comwrote:
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];
...
}
What is a FOO?

Does it require construction?

Do you call bar() in a loop?

FOO is actually a three-dimensional space vector:

class FOO {
float x, y, z;
FOO() : x_(0), y_(0), z_(0) { };
FOO(float x, float y, float z) : x_(x), y_(y), z_(z) { };
inline FOO& operator+=(cons t FOO& a);
/* Many more inline operators and member functions included */
};

bar() is called many times in a loop.
Apr 12 '07 #7
Jim West wrote:
On 2007-04-12, Ian Collins <ia******@hotma il.comwrote:
>>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];
...
}

What is a FOO?

Does it require construction?

Do you call bar() in a loop?

FOO is actually a three-dimensional space vector:

class FOO {
float x, y, z;
FOO() : x_(0), y_(0), z_(0) { };
FOO(float x, float y, float z) : x_(x), y_(y), z_(z) { };
inline FOO& operator+=(cons t FOO& a);
/* Many more inline operators and member functions included */
};

bar() is called many times in a loop.
So there's your reason - FOO() gets called 30 times for each call of bar().

--
Ian Collins.
Apr 12 '07 #8

"Ian Collins" <ia******@hotma il.comwrote in message
news:58******** *****@mid.indiv idual.net...
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];
...
}
What is a FOO?
Foobar is a universal variable understood to represent whatever is being
discussed.
It's usually used in examples that illustrate concepts and ideas in computer
science.
For instance, a computer science professor may be discussing different file
formats. In this case, he would call the generic-example file foo or foobar,
then list the extensions associated with the file formats (e.g. foobar.txt,
foobar.gif, foobar.exe, foobar.tar).

When foo or foobar is used, everyone understands that these are just
examples, and they don't really exist.
Programmers and administrators also use foo and foobar in a similar context.
Files or program s named with foo or foobar are understood not to be
permanent and will be changed or deleted at anytime.
Foo, bar, and the compound foobar were commonly used at MIT, Stanford and
the Helsinki University of Technology, Finland. Other generic variables are
used other places, but only these three are considered universal.

Does it require construction?

Do you call bar() in a loop?

--
Ian Collins.

Apr 12 '07 #9
GeekBoy wrote:
"Ian Collins" <ia******@hotma il.comwrote in message
news:58******** *****@mid.indiv idual.net...
>>
What is a FOO?

When foo or foobar is used, everyone understands that these are just
examples, and they don't really exist.
Not in this case, if you read the OP's reply.
>
Foo, bar, and the compound foobar were commonly used at MIT, Stanford and
the Helsinki University of Technology, Finland. Other generic variables are
used other places, but only these three are considered universal.
If you haven't done so already, research the origin of the term.
>>
--
Ian Collins.
*Please* don't quote signatures.

--
Ian Collins.
Apr 12 '07 #10

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

Similar topics

33
3051
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
2401
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
2393
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
8374
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
2104
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
1149
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
9632
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
10302
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
10071
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
9925
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 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...
0
8958
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
6723
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
5372
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...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
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

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.