Hey guys,
I've written quite a large piece of code the past few days, and today I've cleaned it up a bit, divided it into functions (I know, bad, should've done it the other way around) etc. etc. The project is a simple 3D thingamadingy, the program is able to display WaveFront .obj files using OpenGL, the GLUT library and C++. The part I was working on these few days was the LOD(Level of detail) algorithm. I'd like to use this thread to optimize that particular part as whole by optimizing each of its functions.
Now, with the program more or less working (working in most cases) I want to optimize some functions. I'll post the functions that get called the most, profiled by gcc/gprof. First, (a part of) the output of the profilation:
Flat profile:
Each sample counts as 0.01 seconds.
no time accumulated
% cumulative self self total
time seconds seconds calls Ts/call Ts/call name
0.00 0.00 0.00 100724734 0.00 0.00 visualPart::notOptimized(unsigned int, std::vector<bool, std::allocator<bool> > const&)
0.00 0.00 0.00 196605 0.00 0.00 visualPart::myVerbosePrintf(char*, ...)
0.00 0.00 0.00 147432 0.00 0.00 visualPart::rotateFour(int*)
0.00 0.00 0.00 73736 0.00 0.00 operator new(unsigned int, void*)
0.00 0.00 0.00 49160 0.00 0.00 vertexMatch::~vertexMatch()
0.00 0.00 0.00 24587 0.00 0.00 visualPart::updateVertexMatches(std::vector<vertexMatch, std::allocator<vertexMatch> >&, std::vector<bool, std::allocator<bool> >&)
0.00 0.00 0.00 24587 0.00 0.00 vertexMatch::vertexMatch()
0.00 0.00 0.00 24573 0.00 0.00 visualPart::optimizeQuad(vertexMatch&, std::vector<bool, std::allocator<bool> >&)
0.00 0.00 0.00 24573 0.00 0.00 vertexMatch::vertexMatch(vertexMatch const&)
(BTW: does anybody know how I can get gcc to also measure the time? It'd be of great value. I'm on WinXP x64, using latest (stable) release of the minGW suite in combination with Code::Blocks)
First one is really simple: Rotate an array of 4 integers by one to the right.
inline int visualPart::rotateFour(int *x) {
unsigned int temp = x[3];
x[3] = x[2];
x[2] = x[1];
x[1] = x[0];
x[0] = temp;
return 0;
}
The next function: Scanning through a vector<bool> for 4 elements, returning true if one is false.
inline bool visualPart::notOptimized(const unsigned int quadNumber, const vector<bool> &optimized) {
unsigned int n = 0;
for (n = 0; n < 4; n++) {
if (!optimized[quadindex[quadNumber*4+n]]) {
return true;
}
}
return false;
}
myVerbosePrintf: A function that behaves exactly like printf, but only does print when the verbose variable is true. Little code as there is, little can be optimized, but do you think 200.000 calls to this "empty" function will slow the program significantly down?
int visualPart::myVerbosePrintf(char *fmt, ...) {
if (!verbose) return 0;
int retval;
va_list ap;
va_start(ap, fmt);
retval = vprintf(fmt, ap);
va_end(ap);
return retval;
}
Fourth on the list is the tons of new[] I'm calling. Now I know from C experience that malloc() and calloc() are "very slow" and that new is almost like a C++ wrapper for malloc(). It might be worth looking into this, but I'm not sure whether I'm the one calling new[] all the time or C++ does so too.
Then a destructor for a struct! Surprised me a bit, but nothing bad. However, I'd like to know if you guys think it's worthwhile to minimize the calls to this destructor. Here's the struct:
struct vertexMatch {
unsigned int x;
unsigned int y;
vector<unsigned int> faces;
};
Then comes one of the core functions which is quite long and takes a bit more understanding of the whole thing, but I'll post it eventually (with explaination what I'm doing, etc. etc.)
Then comes the constructor for that struct. Same question as for its destructor.
...
And its copy constructor. Idem.
Basically I'm asking you to take a look at the functions, if you know special keywords for alignment, if they are still useful nowadays with all the optimizing GCC already does, etc. Anything that will speed these functions up is welcome. If you so much as read it all to this, thank you for reading. xD