views:

95

answers:

3

I have a memory block that is divided into a series of location that can be retrieved and returned by client code.
The method that returns locations back looks like this:

void ReturnLocation(void *address) {
    int location = AddressToLocation(address); // I need the location here
    // some code
    DoSmthA(location);
}

void DoSmthA(int location) {
    // I need the address, but also the location
    void *address = LocationToAddress(location); 
    // do something with the address
    DoSmthB(location);
}

void DoSmthB(int location) {
    // Again, I need the address, but also the location
    void *address = LocationToAddress(location);
    // do something with the address
    DoSmthC(location); // It may go on this way...
}

// ------------------------------------------------------
void* LocationToAddress(int location)
{
    return (void *)((char *)this + HEADER_SIZE + (location * LocationSize));
}

int AddressToLocation(void *address)
{
    return (int)(((__int64)address - HEADER_SIZE - (__int64)this) / LocationSize);
}

My question is: should I pass only the location to the helper methods, or it is faster to pass each time the address too (and not compute it again and again):

void DoSmthA(int location, void *address) { }

or even better to use a struct like this:

struct LocationInfo { int Location; void *Address; };
void DoSmthA(LocationInfo locInfo) { }

This methods may be called million of times and I'm not sure if the operations to compute the address from the location (two additions and one multiplication) are faster or slower than passing a second parameter containing the address.

Thanks in advance!

+2  A: 

Assuming you actually do know that this is a bottleneck (you disocvered this by running a profiler, not just be 'knowing' that its million runs is the bottleneck, right?), the correct solution is to try each and see which one has the best improvement. Or to view the generated assembly and see which one is better, if you are crazy.

Brian
+8  A: 

Profile it. Do what is actually faster in your case, on your compiler and with your code base. Not what was faster in my unrelated test, on my unrelated compiler.

Passing an argument to a function is a pretty cheap operation. A stack push/pop, basically.

Computing the location might be very fast, if the division can be optimized away (depends on the value of LocationSize, and whether it is known at compile-time).

So try both, see which is faster in the real world.

CPU's are complex beasts, and performance is not trivial.

jalf
I'm constantly surprised by how bad I am at estimating which of two options will be faster. Good idea!
James Thompson
+4  A: 

You're getting into the kind of optimization where different combination of CPU/compiler and even memory controler can make a difference. We're talking about 1,000,000's of operations only, unless you're adding a few 0's to it, I don't think you can even measure the difference in performance. So, do whatever is easier for maintenance. Developer time is 100x machine time IMO.

Shing Yip