我有两个窗口来测试使用威胁的性能
我循环播放500
For i As Integer = 1 To 500
'Form 1 scenario
Dim Thread1 As New System.Threading.Thread(AddressOf InsertRecord1)
Thread1.Start(RunningID)
Dim Thread2 As New System.Threading.Thread(AddressOf InsertRecord1)
Thread2.Start(RunningID)
Dim Thread3 As New System.Threading.Thread(AddressOf InsertRecord1)
Thread3.Start(RunningID)
Dim Thread4 As New System.Threading.Thread(AddressOf InsertRecord1)
Thread4.Start(RunningID)
Next
表格2场景
For i As Integer = 1 To 500
'Form 2 scenario
Dim Thread1 As New System.Threading.Thread(AddressOf InsertRecord1)
Thread1.Start(RunningID)
Dim Thread2 As New System.Threading.Thread(AddressOf InsertRecord1)
Thread2.Start(RunningID)
Dim Thread3 As New System.Threading.Thread(AddressOf InsertRecord1)
Thread3.Start(RunningID)
Dim Thread4 As New System.Threading.Thread(AddressOf InsertRecord1)
Thread4.Start(RunningID)
Thread1.Join()
Thread2.Join()
Thread3.Join()
Thread4.Join()
Next
显然情景2比情景1快。为什么??
这是收集的数据
Scenario 1 take (milliseconds): 2578 3188 3078 2984 3250
Scenario 2 take (milliseconds): 1890 1969 1828 1938 1860
答案 0 :(得分:1)
我认为这只是表明你的插页不能很好地扩展。您的第一个scanario启动2000个线程并等待它们完成 - 第二个场景启动4个线程并等待它们完成500次。
使用线程会增加执行的开销,如果所有线程都进入同一个无法处理许多连接的数据库,执行时间会变得更糟,那么本身可能会降低代码速度。也许您可以在没有多个线程的情况下测试您的示例,将调用直接放入循环并查看数字,这可能会更快地运行。