What Happens with the Values of Screen Halves?

1

Why does it happen that the times in drawing a hypotenuse on the screen are not the same from the upper left corner to the center of the screen, which for the center of the screen towards the bottom right of the screen? That is, the times with the following code vary

Example:

On Error Resume Next
Dim B As New Bitmap(2000, 2000)
Dim G As Graphics = Graphics.FromImage(B)
Dim P As Pen = Pens.Black
Dim Respuesta As String = vbNullString
Dim Tiempos As Integer = 0
Dim CuentaVeces As Integer = 0
G.Clear(Color.White)

' Medimos el Tiempo de Dibujar una Hipotenusa en pantalla 1 ( 7 D )
Dim TiempoLinea1 As Date = Now
For CuentaVeces = 0 To 1000
    G.DrawLine(P, 0, 0, 1000, 1000)
Next
Tiempos = Now.Millisecond - TiempoLinea1.Millisecond
Respuesta = Respuesta & "Tiempo 1: " & Tiempos & vbCrLf

' Medimos el Tiempo de Dibujar una Hipotenusa en pantalla 2 ( 7 D )
Dim TiempoLinea2 As Date = Now
For CuentaVeces = 0 To 1000
    G.DrawLine(P, 1000, 1000, 2000, 2000)
Next
Tiempos = Now.Millisecond - TiempoLinea2.Millisecond
Respuesta = Respuesta & "Tiempo 2: " & Tiempos & vbCrLf

MsgBox(Respuesta)
    
asked by Pol Flórez Viciana 18.09.2018 в 10:11
source

1 answer

3

There are a couple of problems with the code that you show us. On the one hand, you are using Now to time the time. To do that, .Net has a specific class, which is Stopwatch that is more accurate.

On the other hand, when you are going to measure processes that take so little time, the way to do it is to make several iterations so that the results are more significant.

I modified your code a bit like this:

Private Function Dibujar() As List(Of Int32)
    On Error Resume Next
    Dim B As New Bitmap(2000, 2000)
    Dim G As Graphics = Graphics.FromImage(B)
    Dim P As Pen = Pens.Black
    Dim Respuesta As String = vbNullString
    Dim Tiempos As Integer = 0
    Dim CuentaVeces As Integer = 0
    G.Clear(Color.White)

    Dim listatiempos As List(Of Int32) = New List(Of Int32)

    ' Medimos el Tiempo de Dibujar una Hipotenusa en pantalla 1 ( 7 D )
    Dim sw As Stopwatch = New Stopwatch()
    sw.Start()

    For CuentaVeces = 0 To 1000
        G.DrawLine(P, 0, 0, 1000, 1000)
    Next
    sw.Stop()
    listatiempos.Add(sw.ElapsedMilliseconds)
    ' Medimos el Tiempo de Dibujar una Hipotenusa en pantalla 2 ( 7 D )
    sw.Reset()
    sw.Start()
    For CuentaVeces = 0 To 1000
        G.DrawLine(P, 1000, 1000, 2000, 2000)
    Next
    listatiempos.Add(sw.ElapsedMilliseconds)
    sw.Stop()
    Return listatiempos
End Function

In this way, the function uses Stopwatch and returns both measurements to be added from the process that calls it, which is the following:

For j = 0 To 9
        Dim tiempo1, tiempo2 As Int32
        tiempo1 = 0
        tiempo2 = 0
        For i = 0 To 100
            Dim listatiempos As List(Of Int32)
            listatiempos = Dibujar()
            tiempo1 += listatiempos.ElementAt(0)
            tiempo2 += listatiempos.ElementAt(1)
        Next
        Debug.Print($"Iteración:{j + 1}{vbCrLf}Tiempo1:{tiempo1}{vbCrLf}Tiempo2:{tiempo2}{vbCrLf}")
    Next

Dibujar is called 100 times and all times t1 and t2 are added. This is done 10 times to get a more consistent statistic.

Results:

Iteración:1
 Tiempo1:2002
 Tiempo2:1991

Iteración:2
 Tiempo1:2029
 Tiempo2:2028

Iteración:3
 Tiempo1:2018
 Tiempo2:2029

Iteración:4
 Tiempo1:2028
 Tiempo2:2043

Iteración:5
 Tiempo1:2043
 Tiempo2:2048

Iteración:6
 Tiempo1:2049
 Tiempo2:2023

Iteración:7
 Tiempo1:1998
 Tiempo2:1984

Iteración:8
 Tiempo1:2000
 Tiempo2:2012

Iteración:9
 Tiempo1:1989
 Tiempo2:2005

Iteración:10
 Tiempo1:2001
 Tiempo2:1982

As you can see, the results are very similar, and sometimes tiempo1 takes longer. and other times it is tiempo2 the slowest.

In summary, we see that the time it takes to execute a code depends on many factors and is never 100% consistent. That is why these performance measures do not usually work for too much and I strongly advise against worrying about these issues in processes that take milliseconds. In the end the time is lost in micro-optimizations that do not usually serve much.

    
answered by 18.09.2018 в 11:38