473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Performance issue (bug?) with .NET 2 ListView

Are there any know bugs with the ListView in .NET 2? I'm having
problems with an application that takes 15 seconds in 1.1, and now
takes over a minute. The code in question uses:

listViewItem = this.listView1.Items.Add(...)

followed by 7 calls to

listViewItem.SubItems.Add(...)

When I comment out this code, the application runs in the same time as
the 1.1 version.

Any suggestions? I can provide the source code if required.

May 31 '06 #1
18 1936
I would suggest a: using the ListViewItem ctor that allows you to specify
the sub-items in one go, and b: using the {Begin|End}Update syntax (around a
block of such) - i.e.

listview.BeginUpdate();
try {
ListViewItem newItem = new ListViewItem(new string[] {
"text1","text2","text3","text4"});
listview.Items.Add(newItem);
// and again...
// and again...
// and again...
// (or use AddRange)
} finally {
listview.EndUpdate();
}

See if that works any quicker... you could also try {Suspend|Resume}Layout.

Marc
May 31 '06 #2
What's happening in .Add(...)?
Please post a complete sample that illustraties the issue, without seeing
any code it's hard to tell what's happening.

Willy.

<mr*********@googlemail.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
| Are there any know bugs with the ListView in .NET 2? I'm having
| problems with an application that takes 15 seconds in 1.1, and now
| takes over a minute. The code in question uses:
|
| listViewItem = this.listView1.Items.Add(...)
|
| followed by 7 calls to
|
| listViewItem.SubItems.Add(...)
|
| When I comment out this code, the application runs in the same time as
| the 1.1 version.
|
| Any suggestions? I can provide the source code if required.
|
May 31 '06 #3
try to add the subitems *before* you add the item to the istview

--
<mr*********@googlemail.com> schrieb im Newsbeitrag
news:11**********************@c74g2000cwc.googlegr oups.com...
Are there any know bugs with the ListView in .NET 2? I'm having
problems with an application that takes 15 seconds in 1.1, and now
takes over a minute. The code in question uses:

listViewItem = this.listView1.Items.Add(...)

followed by 7 calls to

listViewItem.SubItems.Add(...)

When I comment out this code, the application runs in the same time as
the 1.1 version.

Any suggestions? I can provide the source code if required.

May 31 '06 #4
It's over 6000 items I'm adding, here's a snippet:

ListViewItem listViewItem =
this.listView1.Items.Add(e.RequestInfo.IP);
listViewItem.SubItems.Add(e.RequestInfo.DateAndTim e.ToString("U"));
listViewItem.SubItems.Add(e.RequestInfo.TimeDiffer ence+ "");
listViewItem.SubItems.Add(e.RequestInfo.Request);
listViewItem.SubItems.Add(e.RequestInfo.Status+" ");
listViewItem.SubItems.Add(e.RequestInfo.Bytes+ "");
listViewItem.SubItems.Add(e.RequestInfo.Referer);
listViewItem.SubItems.Add(e.RequestInfo.Browser);

which is taken from a small project I'm writing, full source is at:

http://www.gotdotnet.com/Workspaces/...1-dbe878a161b7

May 31 '06 #5
I also tried changing

// ListViewItem listViewItem =
this.listView1.Items.Add(e.RequestInfo.IP);
ListViewItem listViewItem = new ListViewItem(e.RequestInfo.IP);
this.listView1.Items.Add(listViewItem);

and also used BeginUpdate,EndUpdate but with no improvement

May 31 '06 #6
Items.AddRange() will be your friend.

If you use .NET2.0 the VirtualMode of the listview class could also be a
great idea.

--
<mr*********@googlemail.com> schrieb im Newsbeitrag
news:11*********************@j55g2000cwa.googlegro ups.com...
It's over 6000 items I'm adding, here's a snippet:

ListViewItem listViewItem =
this.listView1.Items.Add(e.RequestInfo.IP);
listViewItem.SubItems.Add(e.RequestInfo.DateAndTim e.ToString("U"));
listViewItem.SubItems.Add(e.RequestInfo.TimeDiffer ence+ "");
listViewItem.SubItems.Add(e.RequestInfo.Request);
listViewItem.SubItems.Add(e.RequestInfo.Status+" ");
listViewItem.SubItems.Add(e.RequestInfo.Bytes+ "");
listViewItem.SubItems.Add(e.RequestInfo.Referer);
listViewItem.SubItems.Add(e.RequestInfo.Browser);

which is taken from a small project I'm writing, full source is at:

http://www.gotdotnet.com/Workspaces/...1-dbe878a161b7

May 31 '06 #7
I should've posted the whole method, here it is:

private void OnLineMatched(object sender,LogReaderLineEventArgs e)
{
if ( this.InvokeRequired )
{
OnLineMatchedDelegate d1 = new
OnLineMatchedDelegate(OnLineMatched);
this.Invoke(d1, new object[] { sender, e });
}
else
{
// ListViewItem listViewItem =
this.listView1.Items.Add(e.RequestInfo.IP);
ListViewItem listViewItem = new ListViewItem(e.RequestInfo.IP);
listViewItem.SubItems.Add(e.RequestInfo.DateAndTim e.ToString("U"));
listViewItem.SubItems.Add(e.RequestInfo.TimeDiffer ence + "");
listViewItem.SubItems.Add(e.RequestInfo.Request);
listViewItem.SubItems.Add(e.RequestInfo.Status + " ");
listViewItem.SubItems.Add(e.RequestInfo.Bytes + "");
listViewItem.SubItems.Add(e.RequestInfo.Referer);
listViewItem.SubItems.Add(e.RequestInfo.Browser);
this.listView1.Items.Add(listViewItem);
this.listView1.EndUpdate();

this.statusBar1.Panels[1].Text = e.CurrentLine + "/" +
e.TotalLines;
this.progressBar1.PerformStep();
}
}

public class LogReaderLineEventArgs : EventArgs
{
...
public ServerRequestInfo RequestInfo
{
get
{
return this.serverRequestDetails;
}
}
}

public struct ServerRequestInfo
{
public DateTime DateAndTime;
public string IP;
public string Request;
public int Status;
public string Browser;
public string Referer;
public string Method;
public int TimeDifference;
public int Bytes;
}

May 31 '06 #8
The fact it's working in 15 seconds in 1.1 and 1 minute in .NET 2 seems
to point to something else I feel

cody wrote:
Items.AddRange() will be your friend.

If you use .NET2.0 the VirtualMode of the listview class could also be a
great idea.

--
<mr*********@googlemail.com> schrieb im Newsbeitrag
news:11*********************@j55g2000cwa.googlegro ups.com...
It's over 6000 items I'm adding, here's a snippet:

ListViewItem listViewItem =
this.listView1.Items.Add(e.RequestInfo.IP);
listViewItem.SubItems.Add(e.RequestInfo.DateAndTim e.ToString("U"));
listViewItem.SubItems.Add(e.RequestInfo.TimeDiffer ence+ "");
listViewItem.SubItems.Add(e.RequestInfo.Request);
listViewItem.SubItems.Add(e.RequestInfo.Status+" ");
listViewItem.SubItems.Add(e.RequestInfo.Bytes+ "");
listViewItem.SubItems.Add(e.RequestInfo.Referer);
listViewItem.SubItems.Add(e.RequestInfo.Browser);

which is taken from a small project I'm writing, full source is at:

http://www.gotdotnet.com/Workspaces/...1-dbe878a161b7


May 31 '06 #9
Just a thought:

Do you have any try catches that might be swallowing an exception?
Perhaps an exception is being generated and swallowed.

May 31 '06 #10
Chris Dunaway wrote:
Just a thought:

Do you have any try catches that might be swallowing an exception?
Perhaps an exception is being generated and swallowed.


Spot on! For some reason the following code:

private int ConvertToInt(string input)
{
if ( input != null && input != "" )
{
try
{
int retvalue = Convert.ToInt32(input);
return retvalue;
}
catch
{
return 0;
}
}
else
{
return 0;
}
}

was slowing down the application hugely ("A first chance exception of
System.FormatException was caught") in .NET 2.0 but not 1.1. This might
be the debugger, I didn't check. Anyway as I'm using 2.0 I just changed
the above method to:

private int ConvertToInt(string input)
{
if ( input != null && input != "" )
{
int result = 0;
int.TryParse(input, out result);
return result;
}
else
{
return 0;
}
}

May 31 '06 #11
Just a quick note - for performance and also readability reasons, you should
use string.IsNullOrEmpty() instead of checking for null and string.Empty
equality.

<mr*********@googlemail.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
Chris Dunaway wrote:
Just a thought:

Do you have any try catches that might be swallowing an exception?
Perhaps an exception is being generated and swallowed.


Spot on! For some reason the following code:

private int ConvertToInt(string input)
{
if ( input != null && input != "" )
{
try
{
int retvalue = Convert.ToInt32(input);
return retvalue;
}
catch
{
return 0;
}
}
else
{
return 0;
}
}

was slowing down the application hugely ("A first chance exception of
System.FormatException was caught") in .NET 2.0 but not 1.1. This might
be the debugger, I didn't check. Anyway as I'm using 2.0 I just changed
the above method to:

private int ConvertToInt(string input)
{
if ( input != null && input != "" )
{
int result = 0;
int.TryParse(input, out result);
return result;
}
else
{
return 0;
}
}

May 31 '06 #12
Most likely this was the debugger, then; exceptions are actually damned
fast - but the IDE makes 'em look slow.

You may also be able to simplify this to:

private int ConvertToInt32(string input) {
int result;
int.TryParse(input, out result); // discard returned bool
return result;
}

(I also hacked the name just to be pedantic with the CLR guidelines - but
since this method is private it is purely academic)

Marc
May 31 '06 #13
Lebesgue,

While I agree that IsNullOrEmpty does improve readability, I fail to see
how it is an improvement to performance. The code is pretty much the same
in the IsNullOrEmpty method and what is being done here.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lebesgue" <le******@gmail.com> wrote in message
news:eB**************@TK2MSFTNGP02.phx.gbl...
Just a quick note - for performance and also readability reasons, you
should use string.IsNullOrEmpty() instead of checking for null and
string.Empty equality.

<mr*********@googlemail.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
Chris Dunaway wrote:
Just a thought:

Do you have any try catches that might be swallowing an exception?
Perhaps an exception is being generated and swallowed.


Spot on! For some reason the following code:

private int ConvertToInt(string input)
{
if ( input != null && input != "" )
{
try
{
int retvalue = Convert.ToInt32(input);
return retvalue;
}
catch
{
return 0;
}
}
else
{
return 0;
}
}

was slowing down the application hugely ("A first chance exception of
System.FormatException was caught") in .NET 2.0 but not 1.1. This might
be the debugger, I didn't check. Anyway as I'm using 2.0 I just changed
the above method to:

private int ConvertToInt(string input)
{
if ( input != null && input != "" )
{
int result = 0;
int.TryParse(input, out result);
return result;
}
else
{
return 0;
}
}


May 31 '06 #14
Nicholas,

While I understand that == call is delegated to Equals, which checks for
string length equality in the first place, which has basically the same
effect as IsNullOrEmpty call, according to FxCop rules, it yields execution
of significantly more MSIL instructions, thus should be avoided to achieve
the best peformance [1].

I believe the difference would be very subtle - but there certainly is some,
when it's listed as FxCop rule.

[1] FxCop Documentation 1.312.0:
http://www.gotdotnet.com/team/fxcop/...ingLength.html
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O2**************@TK2MSFTNGP04.phx.gbl...
Lebesgue,

While I agree that IsNullOrEmpty does improve readability, I fail to
see how it is an improvement to performance. The code is pretty much the
same in the IsNullOrEmpty method and what is being done here.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lebesgue" <le******@gmail.com> wrote in message
news:eB**************@TK2MSFTNGP02.phx.gbl...
Just a quick note - for performance and also readability reasons, you
should use string.IsNullOrEmpty() instead of checking for null and
string.Empty equality.

<mr*********@googlemail.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
Chris Dunaway wrote:
Just a thought:

Do you have any try catches that might be swallowing an exception?
Perhaps an exception is being generated and swallowed.

Spot on! For some reason the following code:

private int ConvertToInt(string input)
{
if ( input != null && input != "" )
{
try
{
int retvalue = Convert.ToInt32(input);
return retvalue;
}
catch
{
return 0;
}
}
else
{
return 0;
}
}

was slowing down the application hugely ("A first chance exception of
System.FormatException was caught") in .NET 2.0 but not 1.1. This might
be the debugger, I didn't check. Anyway as I'm using 2.0 I just changed
the above method to:

private int ConvertToInt(string input)
{
if ( input != null && input != "" )
{
int result = 0;
int.TryParse(input, out result);
return result;
}
else
{
return 0;
}
}



May 31 '06 #15
Ok, I see what you are saying, yes, it will delegate to Equals, but
ultimately, the first check in the Equals overload is against the length.

It might be a few more IL statements, but I don't know how much exactly.

In the end, it's a moot point, since the readability aspect definitely
makes it more attractive.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lebesgue" <le******@gmail.com> wrote in message
news:en**************@TK2MSFTNGP03.phx.gbl...
Nicholas,

While I understand that == call is delegated to Equals, which checks
for string length equality in the first place, which has basically the
same effect as IsNullOrEmpty call, according to FxCop rules, it yields
execution of significantly more MSIL instructions, thus should be avoided
to achieve the best peformance [1].

I believe the difference would be very subtle - but there certainly is
some, when it's listed as FxCop rule.

[1] FxCop Documentation 1.312.0:
http://www.gotdotnet.com/team/fxcop/...ingLength.html
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:O2**************@TK2MSFTNGP04.phx.gbl...
Lebesgue,

While I agree that IsNullOrEmpty does improve readability, I fail to
see how it is an improvement to performance. The code is pretty much the
same in the IsNullOrEmpty method and what is being done here.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lebesgue" <le******@gmail.com> wrote in message
news:eB**************@TK2MSFTNGP02.phx.gbl...
Just a quick note - for performance and also readability reasons, you
should use string.IsNullOrEmpty() instead of checking for null and
string.Empty equality.

<mr*********@googlemail.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
Chris Dunaway wrote:
> Just a thought:
>
> Do you have any try catches that might be swallowing an exception?
> Perhaps an exception is being generated and swallowed.

Spot on! For some reason the following code:

private int ConvertToInt(string input)
{
if ( input != null && input != "" )
{
try
{
int retvalue = Convert.ToInt32(input);
return retvalue;
}
catch
{
return 0;
}
}
else
{
return 0;
}
}

was slowing down the application hugely ("A first chance exception of
System.FormatException was caught") in .NET 2.0 but not 1.1. This might
be the debugger, I didn't check. Anyway as I'm using 2.0 I just changed
the above method to:

private int ConvertToInt(string input)
{
if ( input != null && input != "" )
{
int result = 0;
int.TryParse(input, out result);
return result;
}
else
{
return 0;
}
}



May 31 '06 #16
So can anyone shed light here on why the ConvertToInt was taking .NET
2.0 so much longer to run than 1.1? Is it the changes to the VS.NET
debugger? Or something in the framework?

Jun 1 '06 #17
Well, the saga continues. The problem was being veiled by the
ConvertToInt inside the business logic part of the code. I have now
added:
Application.EnableVisualStyles();

inside Main. This is actually the source of my problem. Remove it and
the application runs the same speed as the .NET 1.1. Include it and it
runs 4x as slow.

Source code is at:
http://www.google.com/url?sa=D&q=htt...1-dbe878a161b7

If anyone can spare 5 mins to try running it on .NET 1.1 and .NET 2.0,
and add Application.EnableVisualStyles(); to Main() and see if they get
the same speed difference. Use testlog.log as the example log.

Jun 1 '06 #18
On 31 May 2006 05:59:54 -0700, "mr*********@googlemail.com"
<mr*********@googlemail.com> wrote:
It's over 6000 items I'm adding, here's a snippet:


If you are adding 6000 items I really recommend that you use AddRange.
It is significantly (several times) faster than Add when dealing with
large numbers of items. I don't usually use SubItems.Add, but instead
use the constructor that takes a string array.

Here is a some code (slightly modified for simplicity) from one of my
applications that has a ListView with about 10000 items. It takes
about a second to fill it.
private void FillListView()
{
listView.BeginUpdate();
listView.Items.Clear();

//We add to a List(Array) first because AddRange is a lot faster
//than Add when dealing with lots of elements in ListView
//Also, we don't seem to have to turn off the sorter when adding
//all the elements at once

List<ListViewItem> items = new List<ListViewItem>();
foreach (Episode episode in episodes)
items.Add(CreateEpisodeListViewItem(episode));

listView.Items.AddRange(items.ToArray());

listView.EndUpdate();
}

private static ListViewItem CreateEpisodeListViewItem(Episode
episode)
{
ListViewItem item = new ListViewItem(
new string[]
{
episode.OriginalAirdate.HasValue?
episode.OriginalAirdate.Value.ToShortDateString(): "",
episode.Show.Name,
episode.Season.ToString(),
episode.EpisodeNumber.ToString(),
episode.Title
});
item.Tag = episode;
return item;
}

--
Marcus Andrén
Jun 4 '06 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Mateer | last post by:
Hi, I have been running some queries against a table in a my database and have noted an odd (at least it seems odd to me) performance issue. The table has approximately 5 million rows and...
0
by: Mortisus | last post by:
Hi, I'm running a fulltext query on a ~50000 record mysql database and while performance is usually satisfying - about 0.02 secs per query, i get a critical performance deterioration when looking...
1
by: bob | last post by:
Currently i'm writing some low level communication modules in C++ and is thinking of putting it into a library so that it can be used in C#. My concern is the performance issue when putting C++...
17
by: 57R4N63R | last post by:
I'm currently building a website for one of the client. There has been few errors here and there, but just recently the problem is getting worse. Basically the symptoms is that when the user try...
4
by: Ira Siyal | last post by:
Hi I had a project in VB6 which I upgraded to VB.NET recently. In the app, I am facing some issue with listview control. In the earlier app, I have set the Icons in listview through an...
5
by: Varangian | last post by:
Hi, I have a performance issue question? which is best (in terms of efficiency and performance, I don't care neatness in code)... building an ArrayList of Object Instances using SqlDataReader...
0
by: Shades799 | last post by:
Hi All, I was wondering if any of you could help me with a very difficult problem that I am having. I have an ASP site that works with an Oracle database using an ADODB connection. This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.