0

I just want to calculate the time that Dijkstra function will take to calculate shortest path for source node as the network nodes are in Q

    Do While True
        Dim dist As Integer = Integer.MaxValue
        For i = 1 To Q.Count
            If Q.Item(i).dist < dist Then
                dist = Q.Item(i).dist
                u = Q.Item(i)
            End If
        Next i
        If dist = Integer.MaxValue Then Exit Do 'no more nodes available - done!
        Q.Remove(u.name_t)
        'loop over neighbors of u that are in Q
        For j = 1 To Q.Count
            For Each train In trains
                If train.src.name_t = u.name_t And train.dst.name_t = Q.Item(j).name_t Then
                    alt = u.dist + train.t
                    If alt < Q.Item(j).dist Then
                        Q.Item(j).dist = alt
                        Q.Item(j).prev = u
                    End If
                End If
            Next
        Next
    Loop
AlKobtan
  • 65
  • 8
  • If you want to *measure* the time taken, you could use the [Stopwatch class](https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.110%29.aspx). Or do you actually want to *calculate* the time taken for the individual steps on the processor and add them up? – Andrew Morton May 02 '16 at 13:24
  • I didn't get the difference between them, sorry could you explain more – AlKobtan May 03 '16 at 10:57
  • If you had a wall which was 18 bricks long, you could *calculate* the length with (number of bricks * length per brick + (number of bricks -1) * size of a joint). The number of bricks would represent the number of instructions, and the length of a brick and the size of a joint would represent the time taken per instruction. Or you could *measure* the length of the wall with a tape measure, where the wall represents the code (you do not need to know the individual sizes of the bricks and joints) and the tape measure represents the time to run the code. – Andrew Morton May 03 '16 at 16:33
  • Possible duplicate of [Measuring code execution time](http://stackoverflow.com/questions/16376191/measuring-code-execution-time) – Andrew Morton May 03 '16 at 16:35
  • @AndrewMorton I just want to get the total time to compare it with a modified methodology of Dijkstra ... but the rear thing that I use the stopwatch and the other method and I made the output of time appear on label.text when I minimize or maximize the screen the value keep changing is that normal !!!!! – AlKobtan May 03 '16 at 18:00
  • @AlKobtan It is normal for a piece of code to take different times to run on each run. Remember that you are using a multi-tasking operating system where your code only gets a slice of time to run and has to share the processor with other processes. The chances are that your code takes a negligible amount of time compared to, say, updating the display. Is the time you are measuring something like a few milliseconds or much more, like many seconds? Are you trying to find a slow part of your program? – Andrew Morton May 03 '16 at 18:15
  • @AndrewMorton is minimizing and maximizing the form window run the code again every time ?? I think I want to measure milliseconds to appreciate the method suggested .. its all about 0.5 seconds as maximum – AlKobtan May 03 '16 at 18:22

2 Answers2

1

try with this

Dim starttime As DateTime 
Dim endtime As DateTime
Dim elapsed As Double
starttime = Format(Now(), "hh:mm:ss") 'beginning
'your code here
endtime = Format(Now(), "hh:mm:ss") 'end
Elapsed = DateDiff("s", starttime, endtime)
MsgBox("Finished in " & Elapsed & " seconds")

**EDIT 2 **

Sub time()
    startTime = Timer
    endtime = Timer
    totalTime = Format(finishTime - startTime, "ss")
End Sub
Karthick Gunasekaran
  • 2,697
  • 1
  • 15
  • 25
  • it works thanks for you, can it produce by milliseconds – AlKobtan May 03 '16 at 10:53
  • thanks I modified it to parts of seconds .. thanks you really for your help – AlKobtan May 03 '16 at 11:06
  • kindly mark it as solution by clicking the tick mark available on left side of the answer. so that other users having same questions will useful for them. – Karthick Gunasekaran May 03 '16 at 11:22
  • @Karthick It would be better to use a StopWatch rather than the system time. Please see [Is DateTime.Now the best way to measure a function's performance?](http://stackoverflow.com/q/28637/1115360) – Andrew Morton May 03 '16 at 14:21
  • @AndrewMorton, Thanks for your feedback. updated as per your advice – Karthick Gunasekaran May 03 '16 at 14:29
  • @Karthick Your edit seems to have gone wrong, I was expecting to see something more like `Dim sw As New StopWatch()` `sw.Start()` `'your code here` `sw.Stop()` `MsgBox("Elapsed time: " & sw.ElapsedMilliseconds.ToString())`. – Andrew Morton May 03 '16 at 14:36
  • @Karthick I made the output of time appear on label.text when I minimize or maximize the screen the value keep changing is that normal !!!!! – AlKobtan May 03 '16 at 18:00
  • @Karthick I click the tick mark but it seems I need some reputation to modify the public score of the answer and thanks for your help – AlKobtan May 03 '16 at 18:37
0

Get the system date-time before and after function execution, then calculate the difference.

Kevorkian
  • 430
  • 2
  • 4
  • 14