473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Globals vs passing in by reference

I have a lot of functions that add values to an array. They alos update
a global variable of type int. Currently, I use a global variable to
hold this array. All functions access this array global variable and
add their values. Then they increment the global int variable.

Is it faster to pass the array and the int variable by reference to each
function or just access the global variables?

I have tried using gprof, I did not get anything that suggested either ways.

Thanks in advance for any help.

Nov 14 '05 #1
4 2474
In article <cP******************@bgtnsc05-news.ops.worldnet.att.net>,
hello smith <ih***********@ihatespammers.com> wrote:
I have a lot of functions that add values to an array. They alos update
a global variable of type int. Currently, I use a global variable to
hold this array. All functions access this array global variable and
add their values. Then they increment the global int variable.

Is it faster to pass the array and the int variable by reference to each
function or just access the global variables?

I have tried using gprof, I did not get anything that suggested either ways.


Then most likely it doesn't make a measurable difference.

Does anyone complain that your program is too slow? Will anyone give you
lots of money if it runs faster?
Nov 14 '05 #2
On Wed, 11 Feb 2004 22:14:32 GMT, hello smith
<ih***********@ihatespammers.com> wrote:
I have a lot of functions that add values to an array. They alos update
a global variable of type int. Currently, I use a global variable to
hold this array. All functions access this array global variable and
add their values. Then they increment the global int variable.

Is it faster to pass the array and the int variable by reference to each
function or just access the global variables?

I have tried using gprof, I did not get anything that suggested either ways.

Thanks in advance for any help.


[I wouldn't be surprised if there's a FAQ entry on this, but I feel
like typing ;-) ]

There are two separate issues here, IMO relating to:

1. performance - of lesser impact
2. style/clarity/maintainability - of greater impact (okay, it is a
"compound" issue...)

Performance-wise, passing pointers to both the array (in the form of a
pointer to its first element, probably) and the counter has a cost in
terms of function call overhead (a call, arg passing, and return each
time), and then each access of data in the array or the counter
(within the function) will probably cost "a little bit more" than if
you were accessing the items by their own names in those places where
they're in scope (by virtue of the indirection necessary there.)

I'm not surprised you didn't see much difference when profiling,
though; I'd guess that there's enough other computation going on such
that the extra cost is comparatively negligible.

Which brings me to issue #2: If the program would become more
maintainable by encapsulating all the manipulations of that array (and
its associated counter) within that function, then /you/ have to
decide whether or not the price is worth it under your particular
circumstances.

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #3
hello smith wrote:
I have a lot of functions that add values to an array.
They also update a global variable of type int.
Currently, I use a global variable to hold this array.
All functions access this array global variable and add their values.
Then they increment the global int variable.
That's a *very* bad idea.
Is it faster to pass the array and the int variable
by reference to each function or just access the global variables?

I have tried using gprof.
I did not get anything that suggested either ways.
It sounds like you just answered your own question.
cat main.c #include <stdio.h>

typedef struct Pair {
int* A; // pointer to array
int I; // integer
} Pair;

inline
Pair Pair_create(int* array, int i) {
Pair p;
p.A = array;
p.I = i;
return p;
}

inline
int Pair_total(const Pair* pP) {
return pP->I;
}

inline
Pair* Pair_modify(Pair* pP) {
(pP->A)[pP->I] = pP->I;
++(pP->I);
return pP;
}

int Pair_fprintf(FILE* fp, const Pair* pP) {
int characters = fprintf(fp, "%d\n", pP->I);
for (int j = 0; j < pP->I; ++j)
characters += fprintf(fp, "%d\t%d\n", j, pP->A[j]);
return characters;
}

int main(int argc, char* argv[]) {
const
int n = 256;
int array[n];
Pair p = Pair_create(array, 0);
for (int j = 0; j < n; ++j)
Pair_modify(&p);
Pair_fprintf(stdout, &p);
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c> ./main

256
0 0
1 1
Nov 14 '05 #4
hello smith wrote:
I have a lot of functions that add values to an array. They alos update
a global variable of type int. Currently, I use a global variable to
hold this array. All functions access this array global variable and
add their values. Then they increment the global int variable.


It is the strong belief of many that global variables should be avoided
in nearly all situations. Also, C does *not* pass values by reference.
Performance should be of little concern to you in this particular area.
A program written with a large number of global variables can be very
tedious to maintain.

Nov 14 '05 #5

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

Similar topics

45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
2
by: Ron_Adam | last post by:
Using this example: def whatisx(): try: print 'x is',x except: print 'There is no x'
11
by: Matt | last post by:
Hello, I'm rather new to C++ and have a question about globals. What is the big deal about them? They make things much easier to program because you don't have to worry about all the passing...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
2
by: DFS | last post by:
Not sure whether it's bad programming practice or not, but I have a module of globals I declare in each system: Global ws As Workspace Global db As Database Global td As TableDef Global rs As...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
6
by: MSDNAndi | last post by:
Hi, I get the following warning: "Possibly incorrect assignment to local 'oLockObject' which is the argument to a using or lock statement. The Dispose call or unlocking will happen on the...
3
by: ojorus | last post by:
Hi! Just two short questions..: 1) In a function I want to get the value of a global variable declared outside the function. Should I use "global $variable" or $GLOBALS? Are there any...
17
by: meridian | last post by:
I thought I had 'got' globals but this one seems strange. I want my example function 'doIt' to use and optionally modify a module variable 'gname', so I declare 'global gname' in the function, but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.