Member functions and variables

I've been working on a class project for a few days now where I need to modify and improve a program given to us by the teacher. The program mostly relies on a struct Vector, defined as:
1
2
3
4
5
6
7
struct Vector
{
   float x;
   float y;
   float z;
   float w;
}

He asked us to replace this struct with a simple float[4] and alter all the functions accordingly. He suggested we change the functions, which had a general look of (Vector) v = algAdd(v1,v2) to algAdd(v1,v2,&v), so that there was less time spent doing bit-wise copies to and fro. However, I saw that this could all be a lot tidier if I did it with a bit more C++, i.e., making algAdd() a member of Vector, thus changing it from

1
2
3
4
5
Vector algAdd( Vector v1, Vector v2 ) 
{
  Vector v = {v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w};
  return v;
}


to a member-function of Vector, thus turning it into

1
2
3
4
5
6
7
8
9
void Vector::Add(Vector& v1, Vector& v2)
{
    v[0] = v1.v[0]+v2.v[0];
    v[1] = v1.v[1]+v2.v[1];
    v[2] = v1.v[2]+v2.v[2];
    v[3] = 1;
}

v.Add(v1,v2); //v = v1 + v2 


I've done this to all the functions, changing them from functions that return a value to functions that immediately insert the values into the caller's variables.

However, when I compare the speed of my program vs. the one originally given by the teacher, mine is consistently slower. His version runs at approx. 3 seconds, while mine is approx. 3.25 seconds (in great part due to the answers to another question I put here today). It's not much, sure, but... mine should be faster, not slower. I've in fact altered every single function regarding this struct so that not a one returns a value. They all return void and simply inject the values into the caller.

So now I ask: is this just a mistake on my part? With the example above, should mine be faster (i.e. the mistake is somewhere else)? Am I misunderstanding something regarding member functions? Do they actually create temporary variables and then create bit-wise copies into the real variables? In short, does anyone know what it could be?
Last edited on
It is a difficult thing to say. It's somehow hard to believe that this is so much slower. Are you sure You didn't change something else? Anyway, compiler may optimize v = algAdd(v1,v2) so that it doesn't do the copying (I think..). Member functions definitely don't copy everything. They are very much like normal functions. But that's irrelevant. Generally, You should leave optimization to compiler and focus on making your code clean instead.
Last edited on
Yes, well, unfortunately this assignment is about optimizing the code. I usually don't much care about optimization. If it works, I'm happy. But in this case, my "optimized" code is less efficient than the original. And I've only altered functions that have specifically to do with the class I changed. And they've all been changed to void functions that don't return a value and inject the values directly into the object's member variables.

There's also the fact that the teacher's code is in C, while mine is in C++. Is there any known performance differences between the two, with C being "faster" than C++?

As well, both versions are running in Debug mode (to keep things even), so I don't think there's much optimization being done (in terms of inline'ing, etc).
Last edited on
On a very curious note: I, still depressed over the fact that my "optimized" code is slower than the standard one, decided to cheer myself up by switching over to Release mode.

Evidently, the program ran much faster, going from ~3.2-3.8 seconds to ~0.9 seconds. That cheered me up.

Then reality came crashing down and I thought "yeah, but his version would also be much faster in Release." And so, wishing to depress myself further, I changed the teacher's version over to Release and indeed is was faster as well. It went from ~3.0 seconds to ~1.3 seconds.

My code is slower in Debug but faster in Release. What?
Last edited on
My code is slower in Debug but faster in Release. What?


Did you use any other libraries besides the code you write ? Usually in Debug mode, external C/C++ libraries code tend to sprinkle a lot of debugging info so the library developer can find out what is wrong. They could at certain place, put a for loop to iterate through etc etc. So when you add all such debug code up it will of cuz "slow" down the application overall.

At release mode, all such debugging info are not compiled into the libraries so they will run much faster but that also mean if you encounter a crash, the library developer cannot trouble-shoot at all as all debugging info is not around.
I changed some sscanf()'s into fstream>>'s, but that's only used quickly in the very beginning of the program and is only used once. Inside the actual loop (which loops a few thousand times at least), the libraries used in my teacher's code and mine are the same.
That doesn't change the point. Debug mode generates code that is easier to debug. You don't need to know where, why and what code is generated unless you are writing a compiler.
Oh sure. Obviously Debug mode is slower than Release mode.

What I find odd is that my code is slower than my teacher's in Debug mode (3.5 vs 3.0 seconds), but faster than my teacher's in Release mode (0.9 vs 1.3 seconds).
There's no point in worrying about performance in debug mode, it's irrelevant.
+1 Athar
Topic archived. No new replies allowed.