CListView Population is slow (populate with std::string and doubles)

Posted on 2009-04-23
Medium Priority
Last Modified: 2013-11-20
Dear Experts

I am creating an MFC application where one of my document views is a list view. The data I want to populate the list with is a time series, the header row being the name of the series, the first column being times, and the rest of the columns should be filled with data. In some sense this resembles the view in Excel.

My problem is that populating the listview is extremely slow. The code doing that part is:

void CSeriesView::InsertData()
      LVITEM lvItem;
      int nItem;
      CListCtrl& m_List = GetListCtrl();

      CSeriesDoc *iDoc = GetDocument();
      unsigned int nr = iDoc->iSeries.timelength();
      unsigned int nc = iDoc->iSeries.serieslength();
      for (unsigned int i=0; i<nr; i++)
            lvItem.mask = LVIF_TEXT;
            lvItem.iItem = i;
            lvItem.iSubItem = 0;
            std::stringstream iS;
            iS << iDoc->iSeries.t(i);
            lvItem.pszText = str2lpstr(iS.str());
            nItem = m_List.InsertItem(&lvItem);

            for (unsigned int j=0; j<nc; j++)
                  double d_ = iDoc->iSeries(i,j);
                  std::string s_ = boost::lexical_cast<std::string,double>(d_);
                  m_List.SetItemText(nItem, j+1, str2lpstr(s_));

The iSeries is my time series object, its member function t simply returns the time (boost posix_time) for the current row, str2lpstr is my own function converting a std::string to a lpstr and the boost::lexical_cast is my own clumsy way of converting a double to a std::string.

I guess all this stupid conversion is what slows down my application. But it could of course also be the way I use ListView in general. I hope some of you have an idea or two that would help me out.

Thanks, Peter
Question by:PeterTokeHedenAhlgren
  • 2
LVL 31

Accepted Solution

Zoppo earned 500 total points
ID: 24216690
Hi PeterTokeHedenAhlgren,

how many rows do you insert into the list? Is it slow even if you don't add really much items? If you insert much more items than visible a significant speed increasement can be reached using the list view as a virtual list control. A description including sample code for this can be found here: http://www.codeguru.com/cpp/controls/listview/data/article.php/c4197/


Author Comment

ID: 24216999
Ii intend to add >10000 rows. And you are right. Whenever I use a low number of items, speed is all right.

Thanks a lot Zoppo. You are a true "wizard"
LVL 31

Expert Comment

ID: 24217062

Thanks for the compliment ...

Nice if I could help you.

Have a nice day,

best regards,


Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question