-
Jun 19th, 2015, 01:45 AM
#1
Thread Starter
Member
[RESOLVED] How convert this code listview.Items.Remove to linq help Paul ?
-
Jun 19th, 2015, 06:42 AM
#2
Re: How convert this code listview.Items.Remove to linq help Paul ?
LINQ is not faster. All LINQ under the hood is a for statement so how can it be faster? In fact is alot slower.
LINQ should ne used to make code more readable. In fact in this case it does the opposite.
vb Code:
Public Class Form1 Private Sub RemoveListViewItems() Dim min, max As Integer min = 10 max = 20 Dim query = Me.ListView1.Items.Cast(Of ListViewItem)().Where(Function(lvi) Not lvi.SubItems(1).Text >= min OrElse lvi.SubItems(1).Text <= max) Array.ForEach(query.ToArray, Sub(lvi) Me.ListView1.Items.Remove(lvi) End Sub) End Sub End Class
Last edited by ident; Jun 19th, 2015 at 06:59 AM.
-
Jun 19th, 2015, 07:02 AM
#3
Re: How convert this code listview.Items.Remove to linq help Paul ?
Originally Posted by ident
LINQ is not faster. All LINQ under the hood is a for statement so how can it be faster? In fact is alot slower.
I think that's weird how a Linq foreach on a 1D array is slower than a regular for each on a 1D array. Afterall, both statements are O(n) operations, therefore theoretically both should take the same time.
If you find my contributions helpful then rate them.
-
Jun 19th, 2015, 12:42 PM
#4
Re: How convert this code listview.Items.Remove to linq help Paul ?
IT really depends on how it's constructed... I've actually found a couple cases where LINQ was faster... I think it was due in part to the delayed execution, so it doesn't run the query immediately, but on demand, when I access the results.
That said, I don't think LINQ will be a lot of use, nor is the original code... I'm surprised you're not seeing odd results. If you are on Row 2, and you remove it, what was row 3 becomes row 2, so when you iterate to the next item, you get the new row 3, which is the old 4, actually I'm surprised you're not getting an error about editing the collection while iterating through it.
-tg
-
Jun 19th, 2015, 01:53 PM
#5
Re: How convert this code listview.Items.Remove to linq help Paul ?
Originally Posted by techgnome
IT really depends on how it's constructed... I've actually found a couple cases where LINQ was faster... I think it was due in part to the delayed execution, so it doesn't run the query immediately, but on demand, when I access the results.
-tg
can you show the example? Thats generally interesting on non immediate querys as the line does nothing. there are some linq methods that do sort querys quicker. But sorting is not a for each loop. Which is what the question was asked. interested TG buddy.
-
Jun 19th, 2015, 02:00 PM
#6
Re: How convert this code listview.Items.Remove to linq help Paul ?
I don't know what your Listview items look like, or how many you have, but a quick test I just did (which hopefully is valid), would seem to indicate the bottleneck is the multiple removing of items from the list.
So instead of deciding which items to remove, I thought about doing the reverse and decide what items to keep, adding them to a list.
Then, clear the listview, and add the saved items back in one fell swoop.
In my brief test which added a 1000 items to the list, with a random subitem value which ended up removing about 700 of the items from the list, took about 1.4 seconds.
Changing to clearing the list and adding back the 300 kept items took about 42ms, so was a little over 30 times faster.
Perhaps, assuming the premise is valid, it will speed up your situation.
Code:
Dim min, max As Integer
min = 10
max = 20
Dim KeepList As New List(Of ListViewItem)
For Each lvi As ListViewItem In ListadoView.Items
If lvi.SubItems(1).Text > min And lvi.SubItems(1).Text < max Then
KeepList.Add(lvi)
End If
Next
ListadoView.Items.Clear()
ListadoView.Items.AddRange(KeepList.ToArray)
-
Jun 19th, 2015, 02:12 PM
#7
Re: How convert this code listview.Items.Remove to linq help Paul ?
Good thinking passel. How ever as you have already shown above
To add a set of items to the control in a single operation, use the AddRange method.
-
Jun 19th, 2015, 02:27 PM
#8
Re: How convert this code listview.Items.Remove to linq help Paul ?
I don't know if this has been mentioned, but the problem with repeatedly calling .Remove is that the listview is repainted after each .Remove call.
Passel's solution is the best way i know of...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 19th, 2015, 02:34 PM
#9
Re: How convert this code listview.Items.Remove to linq help Paul ?
Originally Posted by .paul.
I don't know if this has been mentioned, but the problem with repeatedly calling .Remove is that the listview is repainted after each .Remove call.
Passel's solution is the best way i know of...
Since we dont want deferred execution would findall be better suited?
-
Jun 19th, 2015, 04:14 PM
#10
Thread Starter
Member
Re: How convert this code listview.Items.Remove to linq help Paul ?
@Ident, thank you for all your help mate, I am new with LINQ and i thought it was fast, then I was wrong i will learn more of the LINQ, thanks for everything and greetings.
@Toph, thank you for visiting my post
@techgnome, excellent explanation bro, thanks
@passel, impressive response friend, thank you very much in addition to being a code pretty fast, explain it very well, thank you very much for helping me, I resolved my question
@.Paul., You are one of the best with LINQ, thousand thanks for your response!
Without more brothers, i have been with the response of the friend @passel in truth is a very fast code, but I also liked the the friend @ident, thank you all for your time and dedication in my post ... I have solved my problem, salutations to all and that you have a great day.
Att: Andres128
-
Jun 19th, 2015, 04:17 PM
#11
Re: How convert this code listview.Items.Remove to linq help Paul ?
Originally Posted by ident
Since we dont want deferred execution would findall be better suited?
What do you mean by deferred execution?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 19th, 2015, 05:06 PM
#12
Re: How convert this code listview.Items.Remove to linq help Paul ?
Originally Posted by .paul.
What do you mean by deferred execution?
I assume you are joking with me?
-
Jun 19th, 2015, 05:38 PM
#13
Re: How convert this code listview.Items.Remove to linq help Paul ?
Originally Posted by ident
can you show the example? Thats generally interesting on non immediate querys as the line does nothing. there are some linq methods that do sort querys quicker. But sorting is not a for each loop. Which is what the question was asked. interested TG buddy.
Sorry, I don't have an example handy. It was from a previous job where I was tasked with optimizing code...there were several cases where I found some looping going on, which when replaced with a LINQ statement, performance increased. There were also times where I found that the LINQ was the poor performer. I don't think there was much of a rhyme or reason to it. Now that I think about it though, a great deal probably had to do with what the code was doing - looping through ALL of the data, and comparing and only operating on part of the data based on condition. When switching to the LINQ statement, it limits the data being processed, so that the only data used in the loop are the data elements that match the criteria.
Originally Posted by .paul.
What do you mean by deferred execution?
There are some cases where LINQ result produces what's called "Deferred Results" - it can be a tough concept to wrap one's head around because it seems to fly in the face of logic that we're accustomed to.
Consider this:
Code:
Dim a as Integer
a= 5
We fully expect that a will contain 5 at the end of the code... it seems logical... it's what we're used to. But let's say that instead of an integer, we assign an object to a, a list that's the result from a LINQ statement...
Code:
Dim aList as List(of SomeClass)
aList = SomeObject.where(function (c) c.Property = "A Value")
If SomeObject contains a few thousand items in it, the compiler may decide to delay the actual execution of the query until the first time the list is accessed... at which time it will get the first item in the results... if you're familiar with Recordset from Classic ADO, it's like a server-side cursor, and only fetches rows on demand.There's circumstances that cause this to happen, when it does and you're not aware or expecting it, you can get some really odd functionality - like trying to get counts... I forget what those specific criteria are, but if you look up .NET deferred execution, I'm sure you can find articles the go into it in more detail than I can at the moment.
-tg
-
Jun 19th, 2015, 05:58 PM
#14
Re: How convert this code listview.Items.Remove to linq help Paul ?
Originally Posted by ident
I assume you are joking with me?
Originally Posted by passel
Code:
Dim min, max As Integer
min = 10
max = 20
Dim KeepList As New List(Of ListViewItem)
For Each lvi As ListViewItem In ListadoView.Items
If lvi.SubItems(1).Text > min And lvi.SubItems(1).Text < max Then
KeepList.Add(lvi)
End If
Next
ListadoView.Items.Clear()
ListadoView.Items.AddRange(KeepList.ToArray)
I don't see how deferred execution would be a problem. It's a straightforward loop. It'll run as expected.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 19th, 2015, 07:36 PM
#15
Re: [RESOLVED] How convert this code listview.Items.Remove to linq help Paul ?
That's not a LINQ query...
It's LINQ statements & lambdas that use deferred execution.
-tg
-
Jun 19th, 2015, 07:38 PM
#16
Re: [RESOLVED] How convert this code listview.Items.Remove to linq help Paul ?
That's what i thought, and i still don't see how Ident's comment is relevant...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 19th, 2015, 07:47 PM
#17
Re: [RESOLVED] How convert this code listview.Items.Remove to linq help Paul ?
I think it was actually in response to my comment initially about deferred execution. It just happened to follow yours.
On a related note - deferred execution isn't necessarily a bad thing, there are ways to deal with it...you just have to know what to do when you run into those situations. Usually it means returning things into a list, rather than directly into the foreach, or you may have to actually create a specific variable rather than using the iterator variable. I've found that the IDE is really good at alerting you to those cases when they crop up.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|