Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Functions VS Subroutines... which one is faster

Author
Message
Sjakie
21
Years of Service
User Offline
Joined: 17th Dec 2003
Location: Netherlands - Delft
Posted: 8th Jan 2006 23:28
Hey everyone,

i'm dealing with a question about speed. I'm coding a pathfinding Library (for my own use, but i'm making it extremely easy-to-use... so maybe i'll put it in the CodeBase when it's done ). As some of you might know, pathfinding routines are often a very big pile of data and it take's pretty much CPU power to do a few search routines (for people who don't know what pathfinding is: it's just what is says, finding a path between point A and B, avoiding any obstacles)

Well okay, lot of talking but this is my question:
you can use functions to call a bunch of not-in-the-main-loop code, but you can also use subroutines with the gosub command. My current way of using things is that i put everything in functions and use them for my pathfinding. But are subroutines faster, or is there another way of making things more efficient? please let me know



P.s. for the few people who are interested about my pathfinding thing: It will be REALLY easy, something like the collision DLL by nuclearglory, maybe even a bit more easy (A): Just 1 command to initialize the entire pathfinding system. That command looks a bit like
Init_Pathfinding(X amount of tiles on the map, Z amount of tiles on the map, Size of the tile's, Amount of units that will use the pathfinding -> because every unit needs its own path storage array and some other stuff )
after that command, you're ready to go, this pathfinding is extremely usefull for use with matrices.

There will also be a function for pathfinding with multiple units, which works with a queue: all units that need a path are put into a queue, and the queue is then emptied one by one, and every unit that's thrown out of the queue will get a path and starts moving. This is all to prevent the system from calculating a lot of paths in one second, instead the job is spread over 1 or 2 seconds (if your dealing with lots of units, 100 or something, if you use 5 you will hardly notice this queue effect)

Oh yeah, and everything is A* pathfinding for who knows what that is. Damn this PS is long, this entire post is long, i'll f... off already ^^

Thanks for reading, if you're still there..

Juzt a dude who likez progging - Me loves RTS games
Cash Curtis II
20
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 9th Jan 2006 00:47
Early on I had the same concern, when I decided to break up Pong y2k into chunks. I tested functions and subs, and had no loss in speed.

Milkman
19
Years of Service
User Offline
Joined: 30th Nov 2005
Location: United States
Posted: 9th Jan 2006 00:51 Edited at: 9th Jan 2006 00:52
i've heard that subs were faster, but cant prove it. it really all depends on what you need to do. if you want to break up your code to make it easier to read, but still have access to variables from any part, then go with subs. however, if you want to make a more object oriented type of program, go with functions. either way, i dont think you'de see a real difference in speed, as cash curtis said (unless you pass several floats into the function).

"Genius is 1% inspiration and 99% perspiration"
wh1sp3r
21
Years of Service
User Offline
Joined: 28th Sep 2003
Location: Czech republic
Posted: 9th Jan 2006 01:09
try this:





:o) I dont know, if this test is right ... Iam used for this "brainstorm" DDDD

PS: Real programmers aren't afraid of math!.

☺☺ AthlonXP 2500+, Ati Radeon 9600 128MB, dualDDR 512MB 400Mhz CL3.0, SB LIVE!, monitor 19" ☺☺
Kevin Picone
22
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 9th Jan 2006 02:22
Use functions. This type of situation virtually dictates it.

One tip, is to implement some form time sharing into the actual path finding engine. So a user can drop an Path request onto the stack (returns it's handle), and the resolve shares it's processing time (base upon the request priority) between all the open path requests upon the stack.

Ideally, this means that the user can throttle scale / up and down much time (milliseconds) is spent on calculating paths.

Epimetheus
21
Years of Service
User Offline
Joined: 5th Oct 2003
Location: Naples, Florida
Posted: 9th Jan 2006 02:41
if darkbasic makes functions "inline" than functions would be faster, but your exe would be bigger.
re faze
20
Years of Service
User Offline
Joined: 24th Sep 2004
Location: The shores of hell.
Posted: 9th Jan 2006 02:43
i believe that gosubs are faster as there is no moving of data, just a jump to a particular address, but functions are necessary for any sort of structure.

x1b
20
Years of Service
User Offline
Joined: 19th Sep 2004
Location:
Posted: 9th Jan 2006 02:59
I find using functions help me manage my code easier

- Do it, Do it Right, Do it right now..
JoelJ
21
Years of Service
User Offline
Joined: 8th Sep 2003
Location: UTAH
Posted: 9th Jan 2006 03:31
functions are better BECAUSE you can reuse them with different variables.

example:
the distance formula:
distance = (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2
you want to find the distance between two objects ( (10,0,10) and (40,0,20) ) and two other objects ( (14,15,16) and (26,27,28) ) and many many more objects??
you can use GOSUBs:


or you could just reuse the code using functions:



But subs do serve their purpose. I use them to initialize player objects and whatnot

Appointed by Jimmy as "MR. GAME REVIEW WIZARD GUY"
Cash Curtis II
20
Years of Service
User Offline
Joined: 8th Apr 2005
Location: Corpus Christi Texas
Posted: 9th Jan 2006 06:02
I use functions primarily to return or set a value, but they're much more useful when you're using arrays, as arrays are automatically global.

BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 9th Jan 2006 10:30
Definitely functions. Not because of speed, but for portability. Check out this tutorial, which explains why, and also how to acheive 100% portable code.

http://members.thegamecreators.com/tutorialcity/html/newsletter_19.html

.

Kangaroo2 BETA2
21
Years of Service
User Offline
Joined: 23rd Aug 2003
Location: Somerset / UK
Posted: 9th Jan 2006 12:13 Edited at: 9th Jan 2006 12:14
He asked about speed - and for speed theres really no difference in dbpro at all.

Personally I use both, and use them in different situations. If I need to call a job with a return value (ie objecttomake=nextfreeobject() ) or with different variables (ie makeenemyobjectintile(64,2) ) I use a funtion. But if I need to section off a bit of non-changing code to make it neater, easier for me to find, or more easily reusable, I use a subroutine ie:


Its a matter of preference, but its a myth that subroutines are slower, at runtime it really does make little difference if your program is one loop of 100 lines or 100 subroutines of 1 line each, so just program however you feel most comfortable

That said, for any kind of library like this, functiosn seems the most sensible route for clarity to other users


Preorder EA here:http://forum.thegamecreators.com/?m=forum_view&t=67575&b=8&p=0
+ Model Pro out now in Program Announcements!
Sjakie
21
Years of Service
User Offline
Joined: 17th Dec 2003
Location: Netherlands - Delft
Posted: 9th Jan 2006 21:04
Okay guys, this is clear to me: Functions... easy to use with arrays, no (noticable) speed difference, and i'm pretty lazy (A) so this is good for me, because I dont have to change everything to GOSUBS... thanks a lot everyone, this means i keep the advantage of folding functions... which IS pretty useful

Juzt a dude who likez progging - Me loves RTS games
JoelJ
21
Years of Service
User Offline
Joined: 8th Sep 2003
Location: UTAH
Posted: 9th Jan 2006 21:08
Quote: "and for speed theres really no difference in dbpro at all.
"

I really disagree with that.

I changed my UPDATECAMERA function to a sub, so I didn't have to make everything global, or pass all the values, etc. and it KILLED my frame rate

RiiDii
20
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Inatincan
Posted: 10th Jan 2006 13:22
Here I am at 4:00 a.m. in the morning and can't sleep (probably too much caffine in my system). Being a total computer geek, I'm coding. As I am coding, I have a revelation and remember reading this thread, and here's what I came up with:

Variables in functions are local and are therefore "forgotten" once the function is done. This means that section of memory is no longer reserved and available for use again. However, when using subroutines, the variables stick around, which means the memory remains reserved regardless if you need that variable again or not. In small codes, this will probably be insignificant. However, in very large codes, this may impact the speed of the code as more and more variables are declared and more memory is used.


Open MMORPG: It's your game!
BatVink
Moderator
22
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 10th Jan 2006 15:48
Good point RiiDii. If you have a "function" that uses many temporary variables for calculations and storage of values, functions will clear up after themselves.

Never really thought of this issue in that way.

Kangaroo2 BETA2
21
Years of Service
User Offline
Joined: 23rd Aug 2003
Location: Somerset / UK
Posted: 10th Jan 2006 16:14 Edited at: 10th Jan 2006 16:33
"I changed my UPDATECAMERA function to a sub, so I didn't have to make everything global, or pass all the values, etc. and it KILLED my frame rate"

Thats strange - its the same speed either way for me.
I've just written this test program to test this:



with SPACE pressed it'll run a subroutine, without it'll do the exact same but with a function. I get thesame framerate regardless of which is being done (a framerate fluctuating between 380 and 420 while internet, firefox, thunderbird, and media player are running) I didn't notice it being on average faster or slower with subroutines...


Preorder EA here:http://forum.thegamecreators.com/?m=forum_view&t=67575&b=8&p=0
+ Model Pro out now in Program Announcements!
The Nerd
21
Years of Service
User Offline
Joined: 5th Jun 2004
Location: Denmark
Posted: 10th Jan 2006 16:18 Edited at: 10th Jan 2006 16:20
Quote: " He asked about speed - and for speed theres really no difference in dbpro at all."


Yes there is Using Subroutines is faster than functions... But of course functions are perfect if you wanna re-use parts of the code in other projects

I use subroutines in my game project... Because I wanted the speed more than the ability to re-use code from the project... As I don't really have any entetion of doing so.

-The Nerd

Free Particle Engine for DarkBasic Pro:
http://forum.thegamecreators.com/?m=forum_view&t=60141&b=5
Kangaroo2 BETA2
21
Years of Service
User Offline
Joined: 23rd Aug 2003
Location: Somerset / UK
Posted: 10th Jan 2006 16:34
So subs are faster for you, slower for him and the same for me? hmm thats weird - perhaps its to do with cpu or gpu architechture?


Preorder EA here:http://forum.thegamecreators.com/?m=forum_view&t=67575&b=8&p=0
+ Model Pro out now in Program Announcements!
JoelJ
21
Years of Service
User Offline
Joined: 8th Sep 2003
Location: UTAH
Posted: 10th Jan 2006 19:40
function: 42fps
gosub: 38 fps

not much... but I remember changing it before, and it KILLED my frame rate... maybe it was because of something else...

PowerSoft
20
Years of Service
User Offline
Joined: 10th Oct 2004
Location: United Kingdom
Posted: 10th Jan 2006 22:29
it could also lead to using varnames that are in the SUB when you dont mean to.

Kangaroo2 BETA2
21
Years of Service
User Offline
Joined: 23rd Aug 2003
Location: Somerset / UK
Posted: 11th Jan 2006 15:43
Joel - yeah that sounds like a more realistic change Perhaps (as has been pointed out) in the gosubs you were using multiple variables that were rememaining in memory, whereas with functions it ignores them afterwards unless you specify that they are global.


Preorder EA here:http://forum.thegamecreators.com/?m=forum_view&t=67575&b=8&p=0
+ Model Pro out now in Program Announcements!
Dead_Zone
19
Years of Service
User Offline
Joined: 6th Jul 2005
Location:
Posted: 12th Jan 2006 03:36 Edited at: 12th Jan 2006 03:38
im my experience functions are faster, before when i had everything loading like fog objects and my generated matrix through sub commands it cost me about 10-15fps which cause my game to run at 45-50ps when all 4 models were viewed i made a HUGE break through with using functions not only easier but i got a solid 60fps looking at the 4 high poly models that would normally slow me down to 45fps.
Jedi Lord
21
Years of Service
User Offline
Joined: 11th Jun 2004
Location: Jedi Temple
Posted: 12th Jan 2006 04:06
I have a good idea for testing which one is faster.
You make 2 programs that test function and test the other... make them both say hello world and add the timer() then write down on the paper and test them both with timer() and compare.

It simple .

Gnome that cast... AMAZING .

Login to post a reply

Server time is: 2025-06-16 06:35:20
Your offset time is: 2025-06-16 06:35:20