Always watch the program flow
I just might have saved myself 5 hours of searching for inefficiency problems in the code I'm working on now.
The problem I was tasked with was, that a function recalculating values for a large number of items (patients with a certain amount of medication data) was leading to memory problems, crashing, and/or taking unbelievably long. So I relentlessly searched the function in question for inefficient bits, fixed and rewrote, only to never seem to make any progress.
When I started work freshly this morning, I started to debug and look up how long one call of the function in question was taking. Tracing the steps the program took, I noticed something... (code is not the actual program code, but simplified to make the problem clearer)
// Function that is called when the menu item is clicked
void menuFunction() {
while(patient != NULL) {
recalculateValues();
patient = nextPatient;
}
}
void recalculateValues() {
while(patient != NULL) {
// ... do the stuff that it's supposed to do
patient = nextPatient;
}
}
Classic case of a double while loop. A problem I could have fixed in less than 5 minutes, but ended up taking 5 hours.
Always watch the program flow, people!