I've recently written a dynamic program that calculates the similarity (modified edit distance) between two sequences of DNA strands (can be lengthy).
My code is like (not actual code since its an assignment):
while(!file.eof){
string line;
int sizeY, sizeX;
//get first strand
getline(db, line)
//second strand
getline(db, line)
double ** ary = new double[sizeY];
//loop to initialize array
for(i to sizeY)
{
for(i to sizex)
{
pair<string,string> p,d;
p.first = "A";
p.second = "T";
d.first = "G";
d.second = "C";
//do some comparisons
}
}
}
The code above will take approximately 40 minutes to complete on a file with ~2400 lines. If I move the pair p,d and assignments outside the nested for-loop and run the exact same file, it will complete in about ~1 minute.
I've read in other threads that the performance is pretty much the same. I've also compiled it with -O2.
Why is the code above so much slower?