vb.netpicturebox

In VB.Net, both code examples achieve the same thing, but which is better?


I checked all the similar questions but couldn't find an answer. Please could someone advise which is better and why?

I have a Picture Box and in Paint, I am drawing horizontal lines with the following code. I have a lot of other things going on in the Paint Event and would like to know which is faster and which uses the least resources.

        'This draws the lines (Example1)
        Dim w As Integer = 30
        For i As Integer = 1 To 6
            e.Graphics.DrawLine(Pens.Black, Start_OffSet, w, PictureBox1.Width, w)
            w += 30
        Next

        'This draws the lines (Example2)
        e.Graphics.DrawLine(Pens.Black, 0, 30, PictureBox1.Width, 30)
        e.Graphics.DrawLine(Pens.Black, 0, 60, Picturebox1.Width, 60)
        e.Graphics.DrawLine(Pens.Black, 0, 90, PictureBox1.Width, 90)
        e.Graphics.DrawLine(Pens.Black, 0, 120, PictureBox1.Width, 120)
        e.Graphics.DrawLine(Pens.Black, 0, 150, PictureBox1.Width, 150)
        e.Graphics.DrawLine(Pens.Black, 0, 180, PictureBox1.Width, 180) 

I tend to think its the second example as no computation is required, but don't know how to confirm this or not.

Many thanks in advance.


Solution

  • Well, there is a concept in computing science. The term was often called "supervisor" mode.

    In effect, the 99% of your computer time going to be the routines that do the drawing of the code. The code you have to "call" those routines thus will not really matter, nor effect the speed of the drawing.

    The drawing command does not care if you used some REALLY slow programming language - even a non compiled language. Since at the end of the day, both are calling the same "api" based drawing command, and that is the bulk of the computer cost here.

    This is why for example back in the VB5/VB6 days, you could "native" compile your application, or run it as un-compiled (it would run interpretating the p-code generated by the compiler).

    However, to draw a text box, draw a circle, or even place text on your windows screen? The compiled code, and the non compiled code were both calling the SAME windows API code. In effect, 99% of the computer code time spent was in that "supervisor" mode, or calling the os code to draw on the screen, and that is not your code!

    So, for 10 lines of code? Even if that code was to run 1000x slower then the compiled 6 lines of code?

    Well, the speed of executing each line of code does not matter, since BOTH THEN are going to wait for the system drawring command to complete.

    your computer can easy right now run a loop to 1 billion - well under a second.

    So, some loop? The time not signficant. Since 99% of the time is spent in the drawing routine, then your code that calls such routines will not matter.

    Say the loop code was 1000x slower?

    Well, the total drawing time, 99% of it? It is waiting for the drawing routiens to finish - of which both examples are calling.

    So say the whole process took 5 seconds.

    Then you have this for the loop:

      5.0000000001
    

    And then you have this for the pre-compiled routines:

      5.0000000000000000000000001
    

    In other words, in both cases, your code cost is the 6 drawing calls, and the speed of calling those drawing routines can be even 10,000 x slower, but the overall speed of the 2 examples will be the same from a user point of view.

    So, the real answer?

    As a developer, which is easier to maintain, and change? Clearly the first example is, so your code choices are not CPU bound choices, but that of developer cost and time - something that not changed compared to vast increase in CPU power we now have.

    So, from a performance point of view, there are no advantages to be had here, and that's due to having billions of computer cycles available here.

    The time spent running the loop is thus a non issue, but the time waiting for the drawing command is your high computer cost - and the speed of the code calling such routines does not change the speed of how fast the drawing commands run - they run the same speed for each case.

    So, pick which one you like best, since the computer cost is a non issue here.