Globals vs passing in by reference

H

hello smith

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.
 
C

Christian Bau

hello smith said:
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?
 
L

Leor Zolman

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
(e-mail address removed)
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
 
E

E. Robert Tisdale

hello said:
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
.
.
.
255 255
 
F

Fao, Sean

hello said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top