Computer Science Canada

Why you might want to stay away from types...

Author:  copthesaint [ Mon Jan 30, 2012 10:00 am ]
Post subject:  Why you might want to stay away from types...

Unfortionatly I decided to spend the time to see how efficient Types are compared to just plain variables. I made one type called X with values x and y
and two variables callled x,y. When tested 100 times, and even 1000 times. my program shows that turing sets 2 normal types at approx 0.0002ms and turing on my computer sets 2 variables in types at 0.00027 ms.

On my computer this fluxuates about +-0.00003 but I have not yet seen variables slower then types.
Does this mean types are bad? I dont think so, considering its not even one tenth of a ms, but 1 ten thousandth of a ms. Generally you wont have to worry about a 0.0001ms difference. but its just good to know.

Heres a program you can use to try on your computer.



Turing:
const RUNTIMES : int := 199
type X :
    record
        x : int
        y : int
    end record

var x, y : int
var P : X

var times1, times2 : array 0 .. RUNTIMES of int
put "please wait"
delay (0)

var timer : int

for j : 0 .. RUNTIMES
    timer := Time.ElapsedCPU

    for i : 0 .. 199999
        P.x := 0
        P.y := 0
    end for
    times2 (j) := Time.ElapsedCPU - timer

    timer := Time.ElapsedCPU

    for i : 0 .. 199999
        x := 0
        y := 0
    end for
    times1 (j) := Time.ElapsedCPU - timer
    %put "It took Turing: ", times1, "ms to set two integer variables."
    %put "It took Turing: ", times2, "ms to set two integer variables in a type."
end for

var timer1AVG, timer2AVG : real := 0

for i : 0 .. RUNTIMES
    timer1AVG := timer1AVG + times1 (i)
    timer2AVG := timer2AVG + times2 (i)
end for
timer1AVG := timer1AVG / (RUNTIMES + 1)
timer2AVG := timer2AVG / (RUNTIMES + 1)


put "It took Turing: ", timer1AVG, "ms on average to set two deferent variables 200000 times, which is about: 0.000", round (timer1AVG / 2), "ms in time to set two intergers"
put ""
put "It took Turing: ", timer2AVG, "ms on average to set two deferent variables in a type 200000 times, which is about: 0.000", round (timer2AVG / 2), "ms in time to set two intergers"
put ""
put "its not a huge speed difference however this can add up."

Author:  DemonWasp [ Mon Jan 30, 2012 11:58 am ]
Post subject:  RE:Why you might want to stay away from types...

You might also find this interesting:

Turing:

View.Set ("text")

const pervasive TRIALS : int := 10000000

class C_pt
    export var x, var y, test

    var x, y : int

    proc test ()
        for i : 1 .. TRIALS
            x := i
            y := i
            assert x = i
            assert y = i
        end for
    end test
end C_pt

type pt :
    record
        x, y : int
    end record

var x, y : int
var p : pt
var arr : array 1 .. 2 of int

var c : ^C_pt
new c

var startTime, endTime : int

startTime := Time.ElapsedCPU
for i : 1 .. TRIALS
    x := i
    y := i
    assert x = i
    assert y = i
end for
endTime := Time.ElapsedCPU

put "Time on standard variables: ", (endTime - startTime)

startTime := Time.ElapsedCPU
for i : 1 .. TRIALS
    p.x := i
    p.y := i
    assert p.x = i
    assert p.y = i
end for
endTime := Time.ElapsedCPU

put "Time on records variables: ", (endTime - startTime)

startTime := Time.ElapsedCPU
for i : 1 .. TRIALS
    arr (1) := i
    arr (2) := i
    assert arr (1) = i
    assert arr (2) = i
end for
endTime := Time.ElapsedCPU

put "Time on array elements: ", (endTime - startTime)

startTime := Time.ElapsedCPU
for i : 1 .. TRIALS
    c -> x := i
    c -> y := i
    assert c -> x = i
    assert c -> y = i
end for
endTime := Time.ElapsedCPU

put "Time on class members through indirection: ", (endTime - startTime)

startTime := Time.ElapsedCPU
c -> test ()
endTime := Time.ElapsedCPU

put "Time on class members locally: ", (endTime - startTime)

proc locals ()
    var x, y : int
    for i : 1 .. TRIALS
        x := i
        y := i
        assert x = i
        assert y = i
    end for
end locals

startTime := Time.ElapsedCPU
locals ()
endTime := Time.ElapsedCPU

put "Time on procedure-local variables: ", (endTime - startTime)

startTime := Time.ElapsedCPU
for i : 1 .. TRIALS
    var lx : int := i
    var ly : int := i
    assert lx = i
    assert ly = i
end for
endTime := Time.ElapsedCPU

put "Time on loop-local variables: ", (endTime - startTime)

proc globals ()
    for i : 1 .. TRIALS
        x := i
        y := i
        assert x = i
        assert y = i
    end for
end globals

startTime := Time.ElapsedCPU
globals ()
endTime := Time.ElapsedCPU

put "Time on global variables: ", (endTime - startTime)


Example output:
code:

Time on standard variables: 5651
Time on records variables: 6815
Time on array elements: 7478
Time on class members through indirection: 8135
Time on class members locally: 6064
Time on procedure-local variables: 5744
Time on loop-local variables: 5406
Time on global variables: 5670


So the following are all pretty much the same speed: global variables, local class member variables, procedure-local variables, loop-local variables, and global variables from within a method. Record members are somewhat slower, and array elements are quite slow; the slowest of all are accessing class members through indirection (->).

This is just Turing being Turing, though: most other languages will post times of 60ms-600ms for those operations. I have only once encountered a language slower than Turing, and that's interpreted Javascript circa 2005; modern JS is way way faster.

Author:  mirhagk [ Mon Jan 30, 2012 1:13 pm ]
Post subject:  RE:Why you might want to stay away from types...

You gotta remember Turing is like circa 2005

Author:  DemonWasp [ Mon Jan 30, 2012 1:53 pm ]
Post subject:  RE:Why you might want to stay away from types...

I might be thinking of Javascript even earlier, come to think of it. I think this data point is remembered from some benchmark done a long time ago that showed Javascript as being about 2000 times slower than compiled C++ for some code snippet, whereas some of my small tests showed Turing as "only" 100 times slower. I imagine the performance situation is a lot muddier than that, but the point stands: nothing is as slow as Turing.

Author:  mirhagk [ Mon Jan 30, 2012 2:00 pm ]
Post subject:  RE:Why you might want to stay away from types...

I don't know demonwasp, some scripting languages might be slightly slower than turing lol.

Author:  copthesaint [ Mon Jan 30, 2012 4:05 pm ]
Post subject:  Re: Why you might want to stay away from types...

DemonWasp wrote:

So the following are all pretty much the same speed: global variables, local class member variables, procedure-local variables, loop-local variables, and global variables from within a method. Record members are somewhat slower, and array elements are quite slow; the slowest of all are accessing class members through indirection (->).

This is just Turing being Turing, though: most other languages will post times of 60ms-600ms for those operations. I have only once encountered a language slower than Turing, and that's interpreted Javascript circa 2005; modern JS is way way faster.


Thanks for expanding even further. Im sure people will appriciate it, or well at least I do Wink lol

Author:  crossley7 [ Mon Jan 30, 2012 9:31 pm ]
Post subject:  RE:Why you might want to stay away from types...

Well, I have found Filemaker Pro (A program similar to Microsoft Access) to have scripting that is fairly slow, but I have not done comparable tests to see which is slower, but I think it ranks up there.

Then again, it is not a standard programming language and designed that way so it is not a fair comparison either


: