老师好!
下面两段代码,都是遍历后(共2500个股piao名称)新增行,只不过第二段代码是在遍历过程中又以遍历100个为一组再循环遍历。并没有涉及多线程的运用,速度却提高几十倍。
1、是快在哪里了?
2、是不是遍历都可以采用这种方式?
请老师指教!
谢谢!
第一段代码:
Dim obj_down As New System.Net.WebClient()
Dim drs As List(Of DataRow) = DataTables("表D").Select("股piao代码 is not null")
For Each dr As DataRow In drs
Dim data As String = obj_down.DownloadString("http://hq.sinajs.cn/list=" & dr("股piao代码"))
Dim start As Integer = data.IndexOf("""") + 1
Dim en As Integer = data.IndexOf(""";")
Dim datas() As String = data.SubString(start, en - start).Split(",")
dr("股piao名字") = datas(0)
dr("今日开盘") = datas(1)
dr("昨日收盘") = datas(2)
Next
第二段代码:
DataTables("表D").ResumeRedraw
Dim wc As New System.Net.WebClient()
DataTables("表D").StopRedraw
Dim len As Integer = 100
Dim i As Integer = 1
Dim count As Integer = 1
Dim str As String = ""
Dim drs As List(Of DataRow) = DataTables("表D").Select("股piao代码 Is not null")
For Each dr As DataRow In drs
str &= dr("股piao代码") & ","
If i >= len * count OrElse i = drs.Count Then
Output.Show("count:" & count)
Output.Show(len & "-" & count & "-" & len * count)
Output.Show("i:" & i)
Dim data As String = wc.DownloadString("http://hq.sinajs.cn/list=" & str.Trim(",")).Trim()
Dim idx As Integer = iif(i = drs.Count, drs.Count - len*(count-1), len)
Output.Show("idx:" & idx)
For Each d As String In data.Split(";")
If idx >= 0 AndAlso d <> "" Then
Dim r As DataRow = drs(i - idx)
Output.Show("(i - idx):" & (i - idx))
Dim start As Integer = d.IndexOf("""") + 1
Dim en As Integer = d.LastIndexOf("""")
Dim datas() As String = d.SubString(start, en - start).Split(",")
If datas.length >= 4 Then
r("股piao名字") = datas(0)
r("今日开盘") = datas(1)
r("昨日收盘") = datas(2)
End If
End If
idx -= 1
Next
count += 1
str = ""
End If
i += 1
Next
DataTables("表D").ResumeRedraw
[此贴子已经被作者于2020/9/20 20:25:21编辑过]