Help! Populating a listview takes ages!!

  • Thread starter Thread starter Paul Tomlinson
  • Start date Start date
P

Paul Tomlinson

All very simple app which loads a log file, parses it and displays the lines
in a listview.
I was playing about with some largeish files 800K and I noticed that the app
would never return control, it would just sit and consume 100% CPU for
aslong as I could be bothered to wait before I pskill's the app.

Anyway I was playing and found that even the code below takes a matter of
minutes to populate the control:

listView_Details.BeginUpdate();

for( int i = 0; i < 10000; i++ )
{
ListViewItem l = listView_Details.Items.Add( "Hello" );
l.SubItems.Add( "Hello" );
l.SubItems.Add( "Hello" );
l.SubItems.Add( "Hello" );
}

listView_Details.BeginUpdate();

Can somone, please anyone tell me how I can speed this operation up.

Thanks
PT
 
Paul said:
All very simple app which loads a log file, parses it and displays the lines
in a listview.

ListView's Add is O(n^2).
Can somone, please anyone tell me how I can speed this operation up.

As a workaround you can collect all the items in a ListViewItem[] and
use AddRange.
 
What do you mean "ListView's Add is O(n^2)"???

It's a computer science term that (very roughly) means "doing it n times
takes n^2 effort". (n^2 means n*n here, it's just notation).

So if calling Add 1000 times takes roughly 1000t, then calling it 2000
times taks roughly 4000t, and 3000 times takes rougly 9000t, 4000 times
takes 16000t, and your 10000 times would take 100000t.

t is any arbitry timeunit here; ns, ms, ...
 
Back
Top