[Solved] Which one of these is best practice/least computationally expensive?


Test it in a console app. Remove things such as it being in run on the UI thread, and being run inside UI controls’ event handlers. Also, the act of displaying the message is huge overhead, so leave it out as it’s done in each method. All you really want to test is the performance difference between defining variables and assigning calculated values, vs. calculating the values inline with three less variables.

Add a stopwatch to time both methods.

I also added some configuration, so you can decide how many iterations to test, and how many times to average the runs, to smooth out the results.

Dim iterations = Enumerable.Range(1, 16).Select(Function(p) CInt(2 ^ p))
Dim averages = 20
Dim durations As New Dictionary(Of Integer, Dictionary(Of Integer, Double))()
For Each iteration In iterations
    Dim d As New Dictionary(Of Integer, Double)()
    Dim sw As New System.Diagnostics.Stopwatch()
    sw.Start()
    For a = 1 To averages
        For i = 0 To iteration - 1
            Dim s = i & vbNewLine & i * 2 & vbNewLine & i * 3
        Next
    Next
    sw.Stop()
    d.Add(1, sw.ElapsedMilliseconds / averages)
    sw.Restart()
    For a = 1 To averages
        For i = 0 To iteration - 1
            Dim single_score As Integer = 0
            Dim double_score As Integer = 0
            Dim triple_score As Integer = 0
            single_score = i
            double_score = i * 2
            triple_score = i * 3
            Dim s = single_score & vbNewLine & double_score & vbNewLine & triple_score
        Next
    Next
    sw.Stop()
    d.Add(2, sw.ElapsedMilliseconds / averages)
    durations.Add(iteration, d)
Next
For Each iteration In iterations
    Console.WriteLine("Number of iterations: {0}", iteration)
    Console.WriteLine("Method 1: {0:0.0} ms", durations(iteration)(1))
    Console.WriteLine("Method 2: {0:0.0} ms", durations(iteration)(2))
Next
Console.ReadLine()

They are pretty much the same

Number of iterations: 2

Method 1: 0.0 ms

Method 2: 0.0 ms

Number of iterations: 4

Method 1: 0.0 ms

Method 2: 0.0 ms

Number of iterations: 8

Method 1: 0.0 ms

Method 2: 0.0 ms

Number of iterations: 16

Method 1: 0.0 ms

Method 2: 0.0 ms

Number of iterations: 32

Method 1: 0.0 ms

Method 2: 0.0 ms

Number of iterations: 64

Method 1: 0.1 ms

Method 2: 0.1 ms

Number of iterations: 128

Method 1: 0.1 ms

Method 2: 0.4 ms

Number of iterations: 256

Method 1: 0.3 ms

Method 2: 0.2 ms

Number of iterations: 512

Method 1: 0.6 ms

Method 2: 0.4 ms

Number of iterations: 1024

Method 1: 0.7 ms

Method 2: 0.7 ms

Number of iterations: 2048

Method 1: 1.5 ms

Method 2: 1.1 ms

Number of iterations: 4096

Method 1: 2.2 ms

Method 2: 2.2 ms

Number of iterations: 8192

Method 1: 4.3 ms

Method 2: 3.5 ms

Number of iterations: 16384

Method 1: 6.7 ms

Method 2: 6.7 ms

Number of iterations: 32768

Method 1: 13.7 ms

Method 2: 13.4 ms

Number of iterations: 65536

Method 1: 28.7 ms

Method 2: 29.0 ms

solved Which one of these is best practice/least computationally expensive?