0

I'm running a simulation which takes some time to run and I want to improve that. From what I understood, passing a value to a function means that that value is copied which is slow. Is there a way to include some functions in a dedicated file where I won't need to copy the values?

I don't mind to do "Wrong programing" (global variable, public access etc.) to gain speed.

Thanks

Edit: when I started my project, I tested several loops. I counted processor style between the start and the stop of a loop of that kind:

int i = 0;
while (i < 10000000){
  i = doStuff(i); //or doStuff();
}

int doStuff(i){
  return i++;
}

int doStuff(){
  return i++;
}

I'm pretty sure that the doStuff() case was 10 times faster. I have changed my previous code to global variables and direct access ("wrong programming") and it improved my runtime significantly. I tried to use references but I had some inherent problem which prevented me from doing so (I don't remember what it was). Anyhow, I'm playing around with gprof now.

Yotam
  • 10,295
  • 30
  • 88
  • 128
  • Look up inlining of function - its something most modern compilers will try to do if possible. – Matthias Wandel Apr 29 '11 at 20:28
  • 6
    Put [optimization] [c++] in the search box and read up. But first find out about profiling. – Zan Lynx Apr 29 '11 at 20:28
  • You could look into inline functions, also passing data to functions by reference rather than passing by value. But first run a profiler to pinpoint the *real* bottlenecks, not the perceived ones. – FrustratedWithFormsDesigner Apr 29 '11 at 20:28
  • 3
    It's entirely possible that passing arguments by value has nothing (or nothing much) to do with your performance problems. You need to do some analysis/profiling to figure out what the issues really might be. Relying on a non-specific rule of thumb that pass-by-value is slow may well lead you down a non-productive path. – Michael Burr Apr 29 '11 at 20:35
  • Which C++ book are you learning from? –  Apr 29 '11 at 20:36
  • Don't do "wrong programming". What's wrong about global variables and many other constructs is that they have the reputation of being fast among people who don't know how to write truly fast code. Correctness and speed often go hand in hand. – Fred Foo Apr 29 '11 at 20:37
  • Cheese cake to the first person to come up with a case where using a global variable actually sped up the program (significantly) – sehe Apr 29 '11 at 20:47
  • Can we see some code, please....? – sehe Apr 29 '11 at 20:48
  • Without profiling you cannot get an answer to any optimization problem. Without even stating the problem, you cannot even get hints. An answer (guaranteed speed improvement) if you only care about speed (and don't mind *correctness*) is not to call the functions at all... the result will not be the correct, but it will be darn fast. Another more serious answer, even better than that is improve the algorithm that drives the functions... anything beyond that is just guessing. – David Rodríguez - dribeas Apr 29 '11 at 21:27
  • OK, I've started to use gprof, if I call a function which calls many other functions, will it consider all the small functions runtime as the runtime the large time? – Yotam Apr 30 '11 at 11:45

2 Answers2

6

You could use references. If you have some function:

void Examine(BigHairyObject o) {
    cout << o.data[1234]; /* for example */
}

you can avoid a copy by passing a reference to that object:

void Examine(BigHairyObject & o) {
    cout << o.data[1234]; /* use is identical */
}

The downside of this, is that you are only referring to the original object, not some copy of it. So, if you modify o (in this example), you are actually modifying the caller's object.

If you want to promise not to modify the object (and in that case, a reference is usually exactly as good as a copy), use the const keyword:

void Examine(const BigHairyObject & o) {
    cout << o.data[1234]; /* use is identical */
    // o.data[1234] = 5; // would cause compile error.
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
2

The way to approach performance tuning is to let the running program tell you what you should optimize. It's probably different from what you are thinking, and there's probably more than one thing you can fix.

If your guess is correct, that it's got to do with function calls and global variables, I'd be surprised. But, regardless, the program itself can tell you.

Here's an example that demonstrates what I mean. It's a simulation program that gets a very large speedup by fixing things that could not have been guessed.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135