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 Discussion / Things like this are why I'm increasingly moving towards DBP

Author
Message
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 12th Feb 2013 00:55
The following code should use recursion to sort three values:


This works perfectly in DBP but in DBC the values overwrite each other. I don't understand this and can only assume it's an issue with DBC.

^ That's what she said.
Kevin Picone
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 12th Feb 2013 05:25 Edited at: 12th Feb 2013 05:28
Haven't tested it, but suspect such an issue could occur because functions in DB classic use static local variables. So passing C in A's place etc are overwriting the existing instances and not a new instance of that functions scope.

Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 12th Feb 2013 08:46
Quote: "Things like this are why I'm increasingly moving towards DBP"

It's free! What reason would you possibly have to stick with DBC?

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 12th Feb 2013 12:37
Your calling a function from within it's own function - it shouldn't be a surprise that things are a bit screwy!

The solution, ban yourself from calling functions from within themselves. This goes for DBPro as well - no matter what language your using, it's a bad idea.

Why not just swap them...

If a>b then t=b : b=a : a=t
If a>c then t=c : c=a : a=t
If b>a then t=a : a=b : b=t
If b>c then t=c : c=b : b=t
If c>b then t=b : b=c : c=t
If c>a then t=a : a=c : c=t


I think that would work.

I got a fever, and the only prescription, is more memes.
Silverman
17
Years of Service
User Offline
Joined: 18th Jan 2007
Location: France
Posted: 12th Feb 2013 13:48 Edited at: 12th Feb 2013 13:49
hi,

I think Kevin Picone is right.
So, with this method, your code works well:


@+

DirectX 9.0c (February 2010)/ DBClassic v1.20
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 12th Feb 2013 16:07 Edited at: 12th Feb 2013 16:24
Quote: "ban yourself from calling functions from within themselves... no matter what language your using, it's a bad idea."

Why is this?

I was originally doing it like this:

But I thought the recursive method might be faster. (I still have not tested if this is true.)

Quote: "Haven't tested it, but suspect such an issue could occur because functions in DB classic use static local variables. So passing C in A's place etc are overwriting the existing instances and not a new instance of that functions scope."

I thought this too but the behaviour is very strange, the parameters to the left of the smallest value get overwritten by it and nothing else happens. It's as if the code were:


@Silverman
Thanks, I'll have to test how fast that is.

^ That's what she said.
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 12th Feb 2013 18:23
Quote: "But I thought the recursive method might be faster. (I still have not tested if this is true.)"

I don't think so. You still have the same number of IF statements, so I'd think swapping a variables would be quicker than creating more overhead with recursive function calls.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 12th Feb 2013 19:49
Quote: " I'd think swapping a variables would be quicker than creating more overhead with recursive function calls."

Quote: "If a>b then t=b : b=a : a=t"

Let's test all three methods...

Here's the code I used for the test:


For 1M iterations:
Temp swap method took 272ms
Straight swap method took 314ms
Recursion method took 881ms

I'm surprised recursion is that slow!

I don't know why I've been using the "straight swap" method for so long, I guess I thought it was clever.

^ That's what she said.
Silverman
17
Years of Service
User Offline
Joined: 18th Jan 2007
Location: France
Posted: 12th Feb 2013 21:43 Edited at: 12th Feb 2013 21:45
The slowness of the recursion is not surprising, DBC must memorize variables (internal) local whenever the function is called itself.
If you are only 3 variables to sort, you can make an optimization, like this:


DirectX 9.0c (February 2010)/ DBClassic v1.20
epicgamer
11
Years of Service
User Offline
Joined: 11th Nov 2012
Location: Unknown
Posted: 23rd Feb 2013 19:48 Edited at: 23rd Feb 2013 20:05
Quote: "the values overwrite each other"


This is common in many languages.
If a>5 then a=3
If a<5 then a=5

This is the reason for SELECT CASE.



Quote: " recursion "


Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd Feb 2013 21:27
Quote: "This is common in many languages.
If a>5 then a=3
If a<5 then a=5"

But doesn't each function call declare its own variables? Calling num(a,b) where
should not be the same as a=b : b=a.


epicgamer
11
Years of Service
User Offline
Joined: 11th Nov 2012
Location: Unknown
Posted: 23rd Feb 2013 21:34 Edited at: 23rd Feb 2013 23:52
flipflop




epicgamer
11
Years of Service
User Offline
Joined: 11th Nov 2012
Location: Unknown
Posted: 23rd Feb 2013 21:46 Edited at: 24th Feb 2013 01:48



Login to post a reply

Server time is: 2024-03-29 12:57:17
Your offset time is: 2024-03-29 12:57:17