
-----------------------------------
Tony
Mon Jul 10, 2006 11:48 pm

[Tutorial] Benchmarking Draw.ThickLine with Turing
-----------------------------------
Benchmarking with Turing

In computing, a benchmark is the result of running a computer program, or a set of programs, in order to assess the relative performance of an object, by running a number of standard tests and trials against it.
What is the best way of performing a certain action? On an introductory level it doesn't matter - whatever works will suffice. "It is assumed that you know your 

A question has been But also, making your own Draw.ThickLine() probably won't give you results as good as the one Turing already has, speed-wise.
Why not? I read somewhere that a lot of turing is made using turing code, so why can't i just tweak it a bit and have it just as fast? Like, sure, maaaybe they coded Draw.ThickLine() in assembly or C, but I ighly doubt it, especially considering how it's a fairly new addition.

I wanted to find out.

The Set Up
{

var x1,x2,y1,y2:int
var t_start, t_end:int

%test out original

t_start := Time.Elapsed

for i:1..100
    x1 := Rand.Int(0,300)
    x2 := Rand.Int(0,300)
    y1 := Rand.Int(0,300)
    y2 := Rand.Int(0,300)
    Draw.ThickLine(x1,y1,x2,y2,5,black)
    Draw.ThickLine(x1,y1,x2,y2,3,grey)
end for

t_end := Time.Elapsed - t_start

locate(1,1)
put t_end

The set up usually takes the following form:

variable declarations
and other overhead

start timer

loop

end timer

display results

The larger the test loop - the better average result. We are trying to average out whatever noise and spikes that might have occured during the testing. It is also important to isolate the timing as close to the function we're measuring as possible.

If using Rand.Int, it is essential to have a large enough sample set to normalize the outcome. 100 samples is a fairly poor choice for the above test, you would normally want something that runs for at least a second or more, but the next comparison test is considerably slower and 100 samples is sufficient to demonstrate the difference.
}
Alternate Function
{

proc MyLine(x1,y1,x2,y2,w,c:int)
    var a:real
    var width:int := w div 2

    %calculate angle of the line
    if (x2-x1) = 0 then
        if y2>y1 then
            a:=PI/2
        else
            a:=PI*1.5
        end if
    else
        a:=arctan((y2-y1)/(x2-x1))
        if x2 < x1 then
            a += PI
        end if
    end if

    %for each point on the line
    for i:1..round(Math.Distance(x1,y1,x2,y2))
        Draw.FillOval(round(x1+i*cos(a)),round(y1+i*sin(a)), width, width, c)
    end for

end MyLine

This is my attempt at drawing a ThickLine, by constructing it out of circles. A faster approach would have been to draw a number of lines and just round off the corners, but some pixels were being skipped at certain angles, so the function didn't behave the same.

The benchmark test should be performed right after the original, hoping that the system is in a relativly same state of load.

t_start := Time.Elapsed
for i:1..100
    x1 := Rand.Int(300,600)
    x2 := Rand.Int(300,600)
    y1 := Rand.Int(0,300)
    y2 := Rand.Int(0,300)
    MyLine(x1,y1,x2,y2,5,black)
    MyLine(x1,y1,x2,y2,3,grey)
end for
t_end := Time.Elapsed - t_start
locate(1,10)
put t_end

}
Some Conclusions
http://www.compsci.ca/v2/download.php?id=4333
61 against 1620 - it's not even a contest. Turing's native Draw.ThickLine is much faster to draw. On the other hand I notice that my function results in a more vivid black outline :wink:

-----------------------------------
Cervantes
Tue Jul 11, 2006 7:47 am


-----------------------------------
Nice, Tony.

Though I think it would be a good idea to remove the Rand.Int's from the for loop. Just hardcode the coordinates so the line draws over itself every time. This doesn't affect the drawing speed, so we can safely factor out those Rand.Int's that are common to both tests.

-----------------------------------
Windsurfer
Wed Jul 12, 2006 9:20 pm

Who said this was a test?
-----------------------------------
:wink:  I must say, drawing circles? That's no way to draw a line!  :P 

Okay, so i made another procedure. It's a little less than half of turing's speed (though it can be sped up to faster than turing at the loss of accuracy!)
I'm still working on it. Maybe I can improve it some more.


procedure DrawFillPoly_Int (x1 : array 1 .. * of int, y1 : array 1 .. * of int, clr : int)
    if upper (x1) > upper (y1) then %what is the upper bound
        Draw.FillPolygon (x1, y1, upper (y1), clr)
    else
        Draw.FillPolygon (x1, y1, upper (x1), clr)
    end if
end DrawFillPoly_Int


procedure DrawThickLine (x1, y1, x2, y2, width : real, clr : int)
    %find angle
    const delta_y := (y2 - y1)
    var angle : real
    if x1 