Computer Science Canada Functions VS Procedures |
Author: | copthesaint [ 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.
|
Author: | DemonWasp [ 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):
This one gives some interesting results:
Which is to say that there's pretty well no difference between function-with-result and procedure-with-reference-argument. |
Author: | mirhagk [ 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. |
Author: | DemonWasp [ 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. |
Author: | copthesaint [ Mon Jan 30, 2012 2:39 pm ] | ||
Post subject: | Re: RE:Functions VS Procedures | ||
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, |
Author: | DemonWasp [ 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. |