Diminuire il tempo di esecuzione di queries LINQ con l’abilitazione del parallelismo è semplice:
Public Class User
Public ID_User As Integer
Public ID_Role As Integer
Public Name As String
Public Surname As String
End Class
Public Class Role
Public ID_Role As Integer
Public Name As String
Public Description As String
End Class
Sub Main()
Dim sw As New Stopwatch
Dim rs As New List(Of Role) From {New Role() With {.ID_Role = 1, .Name = "Default", .Description = ""}, _
New Role() With {.ID_Role = 2, .Name = "J.Dev", .Description = ""}, _
New Role() With {.ID_Role = 3, .Name = "S.Dev", .Description = ""}}
Dim us As New List(Of User) From {New User() With {.ID_User = 1, .ID_Role = 1, .Name = "Fabio", .Surname = "Arosio"}, _
New User() With {.ID_User = 1, .ID_Role = 2, .Name = "Claudio", .Surname = "Arosio"}, _
New User() With {.ID_User = 1, .ID_Role = 1, .Name = "Lorenzo", .Surname = "Arosio"}}
'Uso di una sola CPU
sw.Start()
Dim us_rs = (From p In us Where p.ID_Role = 1 _
Select New With {.User_Name = p.Name, _
.User_Surname = p.Surname, _
.Role = (From q In rs Where q.ID_Role = p.ID_Role Select q.Name).FirstOrDefault _
}).ToList
sw.Stop()
Console.WriteLine("Single CPU Query - {0}", sw.Elapsed.TotalMilliseconds)
'Multi CPU (AsParallel)
sw = New Stopwatch
sw.Start()
Dim Parallel_us_rs = (From p In us Where p.ID_Role = 1 _
Select New With {.User_Name = p.Name, _
.User_Surname = p.Surname, _
.Role = (From q In rs Where q.ID_Role = p.ID_Role Select q.Name).FirstOrDefault _
}).AsParallel.ToList
sw.Stop()
Console.WriteLine("Multi CPU Query - {0}", sw.Elapsed.TotalMilliseconds)
' PrintAnonymousT(us_rs)
Console.ReadLine()
End Sub
Public ID_User As Integer
Public ID_Role As Integer
Public Name As String
Public Surname As String
End Class
Public Class Role
Public ID_Role As Integer
Public Name As String
Public Description As String
End Class
Sub Main()
Dim sw As New Stopwatch
Dim rs As New List(Of Role) From {New Role() With {.ID_Role = 1, .Name = "Default", .Description = ""}, _
New Role() With {.ID_Role = 2, .Name = "J.Dev", .Description = ""}, _
New Role() With {.ID_Role = 3, .Name = "S.Dev", .Description = ""}}
Dim us As New List(Of User) From {New User() With {.ID_User = 1, .ID_Role = 1, .Name = "Fabio", .Surname = "Arosio"}, _
New User() With {.ID_User = 1, .ID_Role = 2, .Name = "Claudio", .Surname = "Arosio"}, _
New User() With {.ID_User = 1, .ID_Role = 1, .Name = "Lorenzo", .Surname = "Arosio"}}
'Uso di una sola CPU
sw.Start()
Dim us_rs = (From p In us Where p.ID_Role = 1 _
Select New With {.User_Name = p.Name, _
.User_Surname = p.Surname, _
.Role = (From q In rs Where q.ID_Role = p.ID_Role Select q.Name).FirstOrDefault _
}).ToList
sw.Stop()
Console.WriteLine("Single CPU Query - {0}", sw.Elapsed.TotalMilliseconds)
'Multi CPU (AsParallel)
sw = New Stopwatch
sw.Start()
Dim Parallel_us_rs = (From p In us Where p.ID_Role = 1 _
Select New With {.User_Name = p.Name, _
.User_Surname = p.Surname, _
.Role = (From q In rs Where q.ID_Role = p.ID_Role Select q.Name).FirstOrDefault _
}).AsParallel.ToList
sw.Stop()
Console.WriteLine("Multi CPU Query - {0}", sw.Elapsed.TotalMilliseconds)
' PrintAnonymousT(us_rs)
Console.ReadLine()
End Sub
3 commenti:
Ora mi toccherà prendere il tuo codice e scriverlo in C#, così abbiamo la riprova se è più veloce VB.Net o C#
- questo risposta è stata fornita da super NERD !!-
Dimenticavo.. mi avevi avvertito che saresti stato sintetico .. non credevo così tanto !!
hai ragione dovrò essere più chiaro.
comunque il parallel spigne una cifra:)
Posta un commento