Search code examples
c++mfcclistctrl

Different time of the execution CListCtrl::InsertItem on the same amount of records


I have a CListCtrl with 10000 records that is filled up when the program starts and time of this operation is ~1.3 sec. But if the user refreshes the list, it fills up in ~2.5 - 3 sec.

In both cases works the same piece of code:

SetRedraw(FALSE);
SetItemCount(nCount);

// insert

SetRedraw(TRUE);

Variable nCount is equal to 0 when the program starts and 10000 when user refreshes the list.

Why does the time of list filling is so different?

UPD: the minimal code

void CTestList::Init()
{
    InsertColumn(0, _T("Number"),   0, 50);
    InsertColumn(1, _T("Obj name"), 0, 150);
    InsertColumn(2, _T("Creator"),  0, 100);
    InsertColumn(3, _T("Editor"),   0, 100);
}

void CTestList::Reset()
{
    LVITEM item;

    item.iItem = 0;
    for (int i = 0; i < 10000; i++)
    {
        InsertRow(item, i);
        item.iItem++;
    }
}

void CTestList::InsertRow(LVITEM& item, int num)
{
    CString strNum;

    //
    item.iSubItem = 0;
    item.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
    item.lParam = NULL;
    item.iImage = 0;

    strNum.Format(_T("%d"), num);
    item.pszText = (LPTSTR)(LPCTSTR)strNum;
    InsertItem(&item);

    //
    item.mask = LVIF_TEXT;
    item.iSubItem = 1;
    item.pszText = _T("Test object");
    SetItem(&item);

    //
    item.mask = LVIF_TEXT;
    item.iSubItem = 2;
    item.pszText = _T("Any one");
    SetItem(&item);

    // 
    item.iSubItem = 3;
    item.pszText = _T("Another one");
    SetItem(&item);
}

void CApp::FillList()
{
    CWaitCursor wait;

    m_list.DeleteAllItems();

    clock_t begin = clock();
    m_list.SetRedraw(FALSE);    
    m_list.SetItemCount(nCount);
    m_list.Reset();
    m_list.SetRedraw(TRUE);
    clock_t end = clock();

    double dif = static_cast<double>(end - begin) / CLOCKS_PER_SEC;

    CString str;

    str.Format(_T("Insertion time: %f"), dif);
    AfxMessageBox(str);
}

Solution

  • I have tested your code on my machine. I can reproduce the different timings only if I run the program in the debugger (using F5), but not if I run it without debugger (using Ctrl+F5). So this seems not to be directly related to your code or Windows API but to the debugger.