Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Functions VS Procedures
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
copthesaint




PostPosted: Mon Jan 30, 2012 10:57 am   Post subject: Functions VS Procedures

I tested both procedures "returning" values versus functions returning values and found that as procedures can set a variable in about 0.00043. HOWEVER functions, those evil things, :S, they takes 0.00054 ms thats 20.4% slower! I ran this a bunch of times on my computer to see how faster procedures can ''return" a value and on average it returns a value in 88-78% of the time it takes for a function to result a value.

Turing:
/*Functions vs Procedures.*/
View.Set ("graphics:640;440,offscreenonly,nobuttonbar")
const RUNTIMES : int := 99

procedure X (var x : int)
    x := 0
end X
function Y : int
    result 0
end Y

var z : int := 0
var timerREC1, timerREC2 : array 0 .. RUNTIMES of int

var timer : int
put "please wait"
for i : 0 .. RUNTIMES
    timer := Time.ElapsedCPU
    for j : 0 .. 199999
        X (z)
    end for
    timerREC1 (i) := Time.ElapsedCPU - timer
    timer := Time.ElapsedCPU
    for j : 0 .. 199999
        z := Y
    end for
    timerREC2 (i) := Time.ElapsedCPU - timer
end for

var timerSUM1, timerSUM2 : real := 0
for i : 0 .. RUNTIMES
    timerSUM1 := timerSUM1 + timerREC1 (i)
    timerSUM2 := timerSUM2 + timerREC2 (i)
end for

timerSUM1 := timerSUM1 / (RUNTIMES + 1)
timerSUM2 := timerSUM2 / (RUNTIMES + 1)


put "It took Turing: ", timerSUM1, "ms to run X 200000 times on average. 0.000", round (timerSUM1 / 2)
put ""
put "It took Turing: ", timerSUM2, "ms to run Y 200000 times on average. 0.000", round (timerSUM2 / 2)
put ""
put "its not a huge speed difference however this can add up."
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Mon Jan 30, 2012 1:50 pm   Post subject: RE:Functions VS Procedures

I rewrote this one too (again, Time.ElapsedCPU isn't especially accurate, so sampling it over a huge number of tests will be more accurate than sampling repeatedly):

Turing:

View.Set ( "text" )

const TRIALS : int := 10000000

procedure X (var x : int, i : int)
    x := i
end X
function Y ( i : int ) : int
    result i
end Y

var z : int := 0

var startTime, endTime : int

startTime := Time.ElapsedCPU
for i : 0 .. TRIALS
    X (z, i)
end for
endTime := Time.ElapsedCPU

put "Proc with variable argument: ", (endTime-startTime), "ms"

startTime := Time.ElapsedCPU
for i : 0 .. TRIALS
    z := Y(i)
end for
endTime := Time.ElapsedCPU

put "Function with result: ", (endTime-startTime), "ms"



This one gives some interesting results:
code:

Proc with variable argument: 5376ms
Function with result: 5381ms


Which is to say that there's pretty well no difference between function-with-result and procedure-with-reference-argument.
mirhagk




PostPosted: Mon Jan 30, 2012 2:03 pm   Post subject: RE:Functions VS Procedures

interesting, because a compiler could optimize the procedure with reference being passed by register instead of on the stack.
DemonWasp




PostPosted: Mon Jan 30, 2012 2:34 pm   Post subject: RE:Functions VS Procedures

"Could" and "does" are usually different. Besides, the version using a return-from-function "could" be inlined and just replaced with an assignment, skipping any jumping at all (a lot faster on pipelined CPUs, which is all of them).

By contrast, Java manages to do this microbenchmark (using return value from a function) in 4ms on the same machine, or 20ms if I also insist that it count how many times it has called x(i), or around 270-1300 times faster. I'm sure you'd get similar results with almost any other language in common use.
copthesaint




PostPosted: Mon Jan 30, 2012 2:39 pm   Post subject: Re: RE:Functions VS Procedures

Turing:

View.Set ("text")

const TRIALS : int := 100000000

procedure X (var x : int, i : int)
    x := i
end X
function Y (i : int) : int
    result i
end Y

var z : int := 0

var startTime, endTime : int
put ""
delay (0)
var times1, times2 : array 0 .. 99 of int

for j : 0 .. 999

    startTime := Time.ElapsedCPU
    for i : 0 .. TRIALS div 1000
        X (z, i)
    end for
    endTime := Time.ElapsedCPU
    times1 (j) := (endTime - startTime)
    %put "Proc with variable argument: ", (endTime - startTime), "ms"

    startTime := Time.ElapsedCPU
    for i : 0 .. TRIALS div 1000
        z := Y (i)
    end for
    endTime := Time.ElapsedCPU
    times2 (j) := (endTime - startTime)

    %put "Function with result: ", (endTime - startTime), "ms"
end for

var time1, time2 : real := 0
for i : 0 .. 999
    time1 := time1 + times1 (i)
    time2 := time2 + times2 (i)
end for

put "Proc with variable argument: ", time1, "ms"
put "Function with result: ", time2, "ms"


I see how your program was more right however you have to take multiple tests of both because of how background programs will affect turing. But yea I see this way that turing is not running one Way faster then the other in your example, sorry about that demon,
DemonWasp




PostPosted: Mon Jan 30, 2012 3:05 pm   Post subject: RE:Functions VS Procedures

There's nothing to be sorry about, and if your test is sufficiently long-running (a few seconds should do it), then background programs shouldn't come into play--assuming you're not turning something computationally expensive on or off at the time. If you just let the program run without prodding anything, it should have fairly consistent results.

Some of your other performance findings were accurate, just not this one.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: