优化使用Long迭代的For循环

时间:2014-07-25 12:25:59

标签: vb.net optimization for-loop

背景故事

我将一个简单的多线程强力哈希黑客程序放在一起,用于求职测试要求。

以下是一些细节

它运行正常,但我的初始版本与此更改部分之间的性能有很大差异。

因子

改变的原因是由于样本数据处理和测试/挑战数据处理之间可能的组合的数量增加。

应用测试样本总共有16 ^ 7个组合..当然少于uint32(或16 ^ 8)。

挑战是一个9长度的散列字符串,它产生一个散列的长值(我得到了);因此它是16 ^ 9。尺寸差异是我所考虑的,这就是为什么我采用简单的方法将初始程序放在一起,针对7长度的Hashed字符串 - 让它在较小的范围内正常运行。

整体 问题不仅仅是增加的组合,由于使用long / int64uint64进行循环操作,因此显着较慢..

当我使用int32(甚至不是uint32)数据类型碾压数字时..我可以听到我的补偿开始了一个档次..整个检​​查在4分钟内完成。这是每个线程的16777216(16 ^ 6)组合检查..

值得注意 - 多线程 我把所有东西都打成了工作线程..其中16个,每个起始字符1个。因此我现在只在每个线程上寻找16 ^ 8组合...这是比uint32值高1个怪异单元(包括0)...

在我提出这段代码之后,我会做出最后的考虑。

功能如下:

Function Propogate() As Boolean
    Propogate = False
    Dim combination As System.Text.StringBuilder = New System.Text.StringBuilder(Me.ScopedLetters)

    For I As Integer = 1 To (Me.ResultLength - Me.ScopedLetters.Length) Step 1
        combination.Append(Me.CombinationLetters.Chars(0))
    Next

    'Benchmarking feedback - This simply adds values to a list to be checked against to denote progress
    Dim ProgressPoint As New List(Of Long)


    '###############################
    '#[THIS IS THE POINT OF INTEREST] 
    '# Me.CombinationLetters = 16    #  
    '# Me.ResultLength = 7 Or 9      #  The 7 was the sample size provided.. 9 was the challenge/test
    '# Me.ScopedLetters.Length = 1   #  In my current implementation 
    '###############################
    Dim TotalCombinations As Long = CType(Me.CombinationLetters.Length ^ (Me.ResultLength - Me.ScopedLetters.Length), Long)

    ProgressPoint.Add(1)
    ProgressPoint.Add(CType(TotalCombinations / 5, Long))
    ProgressPoint.Add(CType(TotalCombinations * 2 / 5, Long))
    ProgressPoint.Add(CType(TotalCombinations * 3 / 5, Long))
    ProgressPoint.Add(CType(TotalCombinations * 4 / 5, Long))
    ProgressPoint.Add(CType(TotalCombinations, Long))

    For I As Long = 1 To TotalCombinations Step 1
        Me.AddKeyHash(combination.ToString) 'The hashing arithmetic and Hash value check is done at this call. 
        Utility.UpdatePosition(Me.CombinationLetters, combination) 'does all the incremental character swapping and string manipulation.. 
        If ProgressPoint.Contains(I) Then
            RaiseEvent OnProgress(CType((I / TotalCombinations) * 100, UInteger).ToString & " - " & Me.Name)
        End If
    Next
    Propogate = True
End Function

我已经知道我可以尝试什么,再次将它放在int32中并在此循环周围放置另一个循环(16次迭代)

但是可能有更好的选择,所以我想听听社区对此的看法。

使用双点精度循环的For循环会更好吗?


顺便说一句,如何耦合是长类型和算术到cpu架构..具体缓存?

我的开发组件很老..奔腾D运行XP专业版x64 ..我的理由是,如果它在我的环境中运行,它可能会在Win Server 2003上运行..

1 个答案:

答案 0 :(得分:0)

最后,这可能是一个硬件问题..我的旧工作站在完成这个项目后不能存活更长时间。

相关问题