Performance Tuning your Code - Loops
Submitted by Date of Submission User Level
Bulent Ozkir Jan 22, 2001 Intermediate
Assume that you have many classic/custom windows controls in a dialog/formview/recordview. If you want to loop and use this values in comparison statements always prefer the second code alternative... ( You will pray the lord! )
--------------------------------------------------------------------------------
// I used m_chk1 as a CheckBox CONTROL variable, and m_bchk1 as a BOOL variable associted with the same checkbox, button1 and button2 are regular push buttons...
// CODE EXCERPTION from only necessary segments...
--------------------------------------------------------------------------------
#define MAX_LOOP 1000000
// using Windows Controls at loops and iterations. Is it wise? NO
void CCheckView::OnButton1()
{
long iFirstTick , iLastTick;
iFirstTick = GetTickCount();
long iCount = 0;
for(long i=0;i if(BST_CHECKED == m_chk1.GetCheck()) iCount++; iLastTick = GetTickCount(); TRACE(_T("Difference:%u\n"), iLastTick - iFirstTick); // I got , Check: Difference:3335 } // using associated member variables instead of Windows Controls at loops and iterations. Is it wise? YES void CCheckView::OnButton2() { long iFirstTick , iLastTick; iFirstTick = GetTickCount(); UpdateData(TRUE); long iCount = 0; for(long i=0;i if(TRUE == m_bchk1) iCount++; iLastTick = GetTickCount(); TRACE(_T("Difference:%u\n"), iLastTick - iFirstTick); // I got , Check: Difference:20 } -------------------------------------------------------------------------------- // button1 - unchecked Check: Difference:3335 Check: iCount:0 // button1 - checked Check: Difference:3335 Check: iCount:1000000 // button2 - unchecked Check: Difference:10 Check: iCount:0 // button2 - checked Check: Difference:10 Check: iCount:1000000 -------------------------------------------------------------------------------- Bulent Ozkir is a Turkey based senior software engineer. He has worked as a senior support engineer for Microsoft from 1998 to 2000. In his over 5 years programming career he has been working on various programming technologies including VB, VC, Visual Interdev, JavaScript, ASP, HTML/DHTML, WMI, COM+, TCP/IP, IIS, SS3, MCIS25, ASP and SQL Server. His background includes B.A. Comp. Sc., MCSE + Internet and MCSD. See members area for more details. --------------------------------------------------------------------------------