执行时间不同 CListCtrl::InsertItem 在相同数量的记录上

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

本文关键字:记录 CListCtrl InsertItem 执行时间      更新时间:2023-10-16

我有一个包含 10000 条记录的 CListCtrl,当程序启动时,此操作的时间是 ~1.3 秒。但是,如果用户刷新列表,它将在 ~2.5 - 3 秒内填满。

在这两种情况下,使用相同的代码段:

SetRedraw(FALSE);
SetItemCount(nCount);
// insert
SetRedraw(TRUE);

变量 nCount 在程序启动时等于 0,当用户刷新列表时等于 10000。

为什么填写清单的时间如此不同?

UPD:最少的代码

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);
}

我已经在我的机器上测试了你的代码。仅当我在调试器中运行程序(使用 F5)时,我才能重现不同的计时,但如果在没有调试器的情况下运行它(使用 Ctrl+F5),则不能重现。因此,这似乎与您的代码或Windows API没有直接关系,而是与调试器有关。