1 pièce(s) jointe(s)
LINQ VS "algo classique" = surprise !
Bonjour à tous,
Voila ce matin je me suis amusé à faire un petit test comparatif des perfs .NEt entre deux méthodes de développement...
Et de manière assez stupéfiante je ne m'attendais pas à ça.
Du coup je voulais vous faire partager histoire de reccueuillir vos avis et voir si quelqu'un a déjà fait d'autres tests et en a déduit une sorte de "best-practice"...
Voici le code testé, tout simple, méthode 1 classique puis méthode 2 et 3 en LINQ.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
Private Sub M1()
Dim initList As New List(Of Integer)
Dim resultList As New List(Of Integer)
For i = 1 To 10000000
initList.Add(i)
Next
For Each i In initList
If i Mod 2 = 0 Then
resultList.Add(i)
End If
Next
End Sub
Private Sub M2()
Dim initList As New List(Of Integer)
Dim resultList As New List(Of Integer)
For i = 1 To 10000000
initList.Add(i)
Next
resultList.AddRange(initList.Where(Function(e) e Mod 2 = 0).ToList)
End Sub
Private Sub M3()
Dim initList As New List(Of Integer)
Dim resultList As New List(Of Integer)
For i = 1 To 10000000
initList.Add(i)
Next
For Each i In initList.Where(Function(e) e Mod 2 = 0)
resultList.Add(i)
Next
End Sub |
Et voici ce qui ressort en résultat. Vous en pensez quoi ?
Pièce jointe 151823