| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2845 人关注过本帖
标题:数组与List,如何提高运算速度
只看楼主 加入收藏
QQ67065302
Rank: 2
等 级:论坛游民
帖 子:3
专家分:20
注 册:2015-5-5
收藏
 问题点数:0 回复次数:1 
数组与List,如何提高运算速度
有两组随机生成的(0~99999)Int32数据A和B,将A按顺序判断在B中是否存在并记录在Boolean型的C中,我分别尝试了Array与List(Of T),在VS2010下以我的破电脑的速度Array大概需要4秒,而List(Of T)则要24秒,以下是我用Array和List(of T)的代码,请高手指点, 顺便问下有无秒杀的方法
     'Array处理
        Dim A As Int32(), b As Int32(), c As Boolean(), i As Int32
        ReDim A(65000), b(66666), c(65000)
        Dim rAd As New System.Random
        Dim Stp As New Stopwatch
        Stp.Start()
        For i = 0 To A.Length - 1
            A(i) = rAd.Next(0, 99999)
        Next
        For i = 0 To b.Length - 1
            b(i) = rAd.Next(0, 99999)
        Next
        For i = 0 To A.Length - 1
            If Array.IndexOf(b, A(i)) >= 0 Then
                c(i) = True
            Else
                c(i) = False
            End If
        Next
        Me.Text = "Array测试" & (Stp.ElapsedMilliseconds) & "毫秒"

 'List(Of T)处理
        Dim A As New List(Of Int32)
        Dim B As New List(Of Int32)
        Dim C As New List(Of Boolean)
        Dim rAd As New System.Random
        Dim Stp As New Stopwatch
        Stp.Start()
        For i As Int32 = 0 To 65000
            A.Add(rAd.Next(0, 99999))
        Next
        For j As Int32 = 0 To 66666
            B.Add(rAd.Next(0, 99999))
        Next
        For k As Int32 = 0 To A.Count - 1
            If B.Contains(A.Item(k)) = True Then
                C.Add(True)
            Else
                C.Add(False)
            End If
        Next
        Me.Text = "list(of int32)测试" & (Stp.ElapsedMilliseconds) & "毫秒"
搜索更多相关主题的帖子: 秒杀 记录 如何 
2015-05-05 08:29
QQ67065302
Rank: 2
等 级:论坛游民
帖 子:3
专家分:20
注 册:2015-5-5
收藏
得分:0 
Private Function Bs(ByVal List() As Integer, ByVal N As Integer) As Integer
        Dim Low As Integer, Up As Integer, Midi As Integer
        Low = LBound(List)
        Up = UBound(List)
        Do While Low <= Up
            Midi = (Low + Up) / 2
            If List(Midi) = N Then
                Bs = Midi
                Exit Do
            ElseIf List(Midi) > N Then
                Up = Midi - 1
            ElseIf List(Midi) < N Then
                Low = Midi + 1
            End If
        Loop
        If Low > Up Then Bs = -1
    End Function
  Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim A As Int32(), b As Int32(), c As Boolean(), i As Int32
        ReDim A(65000), b(66666), c(65000)
        Dim rAd As New System.Random
        Dim Stp As New Stopwatch
        Stp.Start()
        For i = 0 To A.Length - 1
            A(i) = rAd.Next(0, 99999)
        Next
        For i = 0 To b.Length - 1
            b(i) = rAd.Next(0, 99999)
        Next
        Array.Sort(A) '排序与否无所谓,重复与否不影响
        Array.Sort(b)
        For i = 0 To A.Length - 1
            If Bs(A, b(i)) >= 0 Then
                c(i) = True
            Else
                c(i) = False
            End If
        Next
        Me.Text = "二分法测试" & (Stp.ElapsedMilliseconds) & "毫秒"
    End Sub
用二分法差不多80毫秒,基本上满足了
2015-05-05 23:09
快速回复:数组与List,如何提高运算速度
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016159 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved