Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Slowdown in the middle of calculations

Status
Not open for further replies.

aagaa

Programmer
Sep 16, 2002
2
US
I've written a program that I've been using to calculate the distance between atoms when given the coordinates of two atoms. It finds atom pairs that have a minimum separation between each other, but I've recently modified it to display all distances for all the pairs. However, for particularly long files (with many atoms to calculate), the program slows progress to a halt. For smaller files it will start off quickly, slow down in the middle, and then speed up very rapidly in approximately the last 5% of the calculations. I've posted the "body" of the code (the first portion has been ommitted, and only one file in the project has been posted). The bolded portion is where I believe the error is occuring.

Code:
	UpdateData(TRUE);
	m_sOutput="";
	UpdateData(FALSE);
	UpdateData(TRUE);
	char tempChar[80] = "TEMP!";
	char a[80];
	bool correct=false;
	int lines=0;
	double dis;
	CString buffer;
	int index=0;
	ifstream tempFile(m_sFileName, ios::nocreate);
	if(tempFile.fail()) {
		MessageBox("Sorry, there was an error opening the file.","The Atomic Analyzer",MB_OK);
		return;
	}
		while(tempFile.eof()!=1) {
			if(tempChar[0]=='R'&&tempChar[1]=='E'&&tempChar[2]=='M') {
				tempFile.getline(a,999);
			}
			else {
				lines++;
				tempFile.getline(a,999);
			}
		}
		tempFile.close();
		m_cTheAtoms=new CAtom[lines];
	ifstream file(m_sFileName, ios::nocreate);
		if (file.fail()) {
			MessageBox("Sorry, there was an error opening the file.","The Atomic Analyzer",MB_OK);
			return;
		}
		
	MessageBox("This might take a while.","The Atomic Analyzer",MB_OK);
	while(file.eof()!=1) {
		file>>tempChar;
		if(tempChar[0]=='R'&&tempChar[1]=='E'&&tempChar[2]=='M') {
			file.getline(a,999);
		}
		else if(tempChar[0]=='E'&&tempChar[1]=='N'&&tempChar[2]=='D') {
			file.getline(a,999);
		}
		else if(tempChar[0]=='A'&&tempChar[1]=='T'&&tempChar[2]=='O') {
			file>>m_cTheAtoms[index].m_iAtomNum;
			file>>m_cTheAtoms[index].m_sAtomName;
			file>>m_cTheAtoms[index].m_sResName;
			file>>m_cTheAtoms[index].m_iResNum;
			file>>m_cTheAtoms[index].m_fX;
			file>>m_cTheAtoms[index].m_fY;
			file>>m_cTheAtoms[index].m_fZ;
			file>>tempChar;
			file>>tempChar;
			index++;
		}
		else {
			MessageBox("Corrupt Data Detected.  Please check input file.","The Atomic Analyzer",MB_OK);
			return;
		}
Code:
	int atomsFound=0;
	m_cProgress.SetRange(0,index);
	for(int i=0; i<index; i++) {
		m_cProgress.SetPos(i);
		for(int j=(i+1); j<index; j++) {
			dis=sqrt(pow(m_cTheAtoms[j].m_fX-m_cTheAtoms[i].m_fX,2)+pow(m_cTheAtoms[j].m_fY-m_cTheAtoms[i].m_fY,2)+pow(m_cTheAtoms[j].m_fZ-m_cTheAtoms[i].m_fZ,2));
			buffer.Format(&quot;Atom %d (res#%d) to Atom %d (res#%d), Distance: %f\r\n&quot;,m_cTheAtoms[i].m_iAtomNum,m_cTheAtoms[i].m_iResNum,m_cTheAtoms[j].m_iAtomNum,m_cTheAtoms[j].m_iResNum,dis);
			m_sOutput=m_sOutput+buffer;
			atomsFound++;
			UpdateData(FALSE);	
		}
	}
Code:
	file.close();
	if(m_bYesOut==TRUE) {
		time_t currentTime;
		time(&currentTime);
		char tempDate[80];
		struct tm * ptm = localtime(&currentTime);
		sprintf(tempDate,&quot;results__%d-%d__%d-%d-%d.txt&quot;,ptm->tm_mon+1,ptm->tm_mday,ptm->tm_hour,ptm->tm_min+1,ptm->tm_sec+1);                               
		fstream outFile(tempDate,ios::out);
		outFile << m_sOutput << &quot;\n&quot; << atomsFound << &quot; atom pairs were found to meet the max distance (&quot;<<m_iDistance<<&quot;) requirement.&quot;;
		outFile.close();
		ptm = NULL;
		delete ptm;
	}
	m_cTheAtoms=NULL;
	delete m_cTheAtoms;
	UpdateData(FALSE);
	buffer.Format(&quot;%d atom pairs were found to meet the max distance (%f) requirement.&quot;,atomsFound,m_iDistance);
	MessageBox(buffer,&quot;The Atomic Analyzer&quot;,MB_OK);

I've been puzzling over this slowdown for days and I can't figure anything out. Any is greatly appreciated. :D
 
Figuring something like this out with incomplete source and data is very difficult. My suggestion is that you use the VC++ profiler to find out where the program is spending most of its time.

My first suspicion is that this has something to do with memory allocation for the CString, but that's little more than wild guess. There are too many unknowns.

DJ
 
I would look into using threads to do the calculations and in the loop to determine if the thread is done, update the GUI with some text informing the end user that calculation is going on. PLEASE KEEP IN MIND that the thread itself can NOT touch the GUI unless it is a UI Thread and you DONT want to go down that path. All examples provided by MS & everthing I tried has proven to me that it is not the way to go.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top