什么是长数组项目的最快减法方式? [英] What is the fastest way substraction of long array items?
本文介绍了什么是长数组项目的最快减法方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个长阵列
I have a long array
Dim LongArray As Long() = {1595741111714190885, 2681354977222731062, 3897343441765742102, 1813043105221908777, 2970433094069856569, ...}
我想减去下一个项目中的每个项目。
I want subtract each item from next item.
Dim DiffArray As New List(Of Long)
For i As Integer = 0 To LongArray.Count - 1
Dim Diff As Long = LongArray(i + 1) - LongArray(i)
DiffArray.Add(Diff)
Next
但这种方式太慢了。
我尝试了什么:
FOR NEXT
But this way too slow.
What I have tried:
FOR NEXT
Dim LongArray As Long() = {1595741111714190885, 2681354977222731062, 3897343441765742102, 1813043105221908777, 2970433094069856569}
Dim DiffArray As New List(Of Long)
For i As Integer = 0 To LongArray.Count - 1
Dim Diff As Long = LongArray(i + 1) - LongArray(i)
DiffArray.Add(Diff)
Next
LINQ
LINQ
Dim DiffArray2 = (From x In LongArray Let nextindex = LongArray.ToList.IndexOf(x) + 1 Let nextelement = LongArray.ToList.ElementAt(If(nextindex = LongArray.ToList.Count, nextindex - 1, nextindex)) Select nextelement - x).ToList()
推荐答案
Dave现货......举个例子,我使用1.Dave的代码运行10,000个项目的基准测试。 2. Linq版; 3. PLinq版本。以下是代码:
Dave is spot on... To give you an example, I ran a benchmark on 10,000 items using 1. Dave's code; 2. a Linq version; 3. a PLinq version. Below is the code:
<Benchmark(Baseline:=True)>
Public Sub Execute()
Dim diffArray = New Long(maxSize - 1 - 1) {}
For i As Integer = 0 To diffArray.Length - 1
diffArray(i) = LongArray(i + 1) - LongArray(i)
Next
End Sub
<Benchmark>
Public Sub ExecuteLinq()
Dim diffArray As Long() = LongArray.Skip(1).[Select](Function(x, i) LongArray(i) - x).ToArray()
End Sub
<Benchmark>
Public Sub ExecutePLinq()
Dim diffArray As Long() = LongArray.Skip(1).[Select](Function(x, i) LongArray(i) - x).AsParallel().AsOrdered().ToArray()
End Sub
以下是基准测试结果:
And here are the benchmark results:
// * Summary *
BenchmarkDotNet=v0.11.1, OS=Windows 10.0.17134.228 (1803/April2018Update/Redstone4)
Intel Core i7-4980HQ CPU 2.80GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
[Host] : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.7.3132.0
DefaultJob : .NET Framework 4.7.2 (CLR 4.0.30319.42000), 32bit LegacyJIT-v4.7.3132.0
Method | Mean | Error | StdDev | Scaled | ScaledSD |
------------- |------------:|----------:|----------:|-------:|---------:|
Execute | 22.02 us | 0.2142 us | 0.2003 us | 1.00 | 0.00 |
ExecuteLinq | 297.39 us | 1.6327 us | 1.5272 us | 13.51 | 0.14 |
ExecutePLinq | 1,210.37 us | 6.4961 us | 5.7586 us | 54.98 | 0.54 |
// * Legends *
Mean : Arithmetic mean of all measurements
Error : Half of 99.9% confidence interval
StdDev : Standard deviation of all measurements
Scaled : Mean(CurrentBenchmark) / Mean(BaselineBenchmark)
ScaledSD : Standard deviation of ratio of distribution of [CurrentBenchmark] and [BaselineBenchmark]
1 us : 1 Microsecond (0.000001 sec)
希望这会有所帮助! :)
Hope this helps! :)
列表会让事情变得缓慢。
你最快的运行速度是你的Fox / Next循环,但首先,将你的DiffArray预先分配到保存你要计算的所有值所需的大小,这应该是你的LongArray的大小 - 1.
Lists are going to slow things down a bit.
The quickest you're going to get this to run is your Fox/Next loop, but first, preallocate your DiffArray to the size required to hold all the values you're going to calculate, which should be the size of your LongArray - 1.
Dim DiffArray(LongArray.Count - 1) As Long
For i As Integer = 0 To LongArray.Count - 1
Dim Diff As Long = LongArray(i + 1) - LongArray(i)
DiffArray(i) = Diff
Next
这篇关于什么是长数组项目的最快减法方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文