Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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

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
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()
Posted
Updated 23-Aug-18 18:07pm
v2
Comments
Patrice T 23-Aug-18 18:43pm    
What exactly are you trying to do ?
Your code is not only doing subtractions.

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:
VB
<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! :)
 
Share this answer
 
Comments
gacar 24-Aug-18 5:03am    
Graeme_Grant, thaks for solution. Your first solution fastest. And also thanks for linq solutions.
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.
VB.NET
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
 
Share this answer
 
Comments
Jochen Arndt 24-Aug-18 4:49am    
Should be
For i As Integer = 0 To DiffArray.Count - 1
or
For i As Integer = 0 To LongArray.Count - 2

Countered the 1 vote anyway.
gacar 24-Aug-18 4:56am    
Yes, right.
Dave Kreskowiak 24-Aug-18 8:44am    
Thanks! It was late, and my brain was turned off. :)
gacar 24-Aug-18 5:02am    
Dave Kreskowiak, thaks for solution. Yes, very speed now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900