Which Is Faster?
Miscellaneous Forums/General Discussion/Which Is Faster?
which is faster if-then-else or select-case branches?
--Mike
From what I can recall, doesn't Blitz break down select-case to if-then-else statements behind-the-scenes? If so, it's the same speed.
I don't think you would notice the speed difference, even if there was one.
Agree with Ross C, if you're looking to optimise your game, this isn't the kind of thing you need to be focusing on :)
To (mis)quote Warren, if you've reached the stage where you're optimizing if-then-else, it's done, ship it.
LOL... i wish... ok, what about key presses... i mean,if someone is holding down the arrow key, to move the view, for a few seconds, do these accumulate somewhere... should i call flushkeys() somewhere in the main loop... i'm noticing a stutterring and general slowdown of the app after a few minutes...
i'm checking teh entire logic... especially the particle stuff, just to make sure all particles are being deleted...
... have i gone completely bonkers and am reaching for straws now... halp me somebody!!!!
thx for all suggestions and thoughts...
--Mike
Sounds like it could be a memory leak, keep an eye on your memory usage in windows task manager while your app is running idle... if it's constantly rising, you're in trouble ;)
Keypresses are stored in the keyboard buffer, which is why in da old days, when yoo kept yer finger on the key and then removed it, the cursors still went flying along.
This is also what brought shrieks of alarm and panic from my studenst wne they did the same thing with the backspace key and watched an hours work wipe itself :)
Cheers
Garion
ahhh, good idea Jams... thx
god i wish we had a proper debugger/trace facility... knowing me, it's probably a dumb mistake somewhere right in front of me...
this is why i'm so hot on OOP stuff... without it i'd be jumping up and down cursing now trying to pin down this thing :)
'scuse me, i need a smoke break :)
--Mike
OT: Red, Did you recieve my email this time?
yes... thx Cyg, will look at it in a few...
--Mike
Red,
I do actually seem to remember it has been stated on these very forums that SELECT CASE is faster than a lot of IF Then statements, but I think you need a heck of a lot to notice the difference.
IPete2.
I come across alot of these situations myself while coding
and by far, the best way to get answers is always to try it yourself.
I would write a small program that runs for exactly 1 or 2
seconds. I would then put the if-then or select-case code
inside. Count how many times it loops and print it off.
There! You have your answer.
Pretty straight forward.
Local a:Int=1
Local stime:Int
Local etime:Int
stime=MilliSecs()
For Local i:Int = 1 To 1000000000
If a=2
Else
If a=3
Else
If a=4
Else
End If
End If
End If
Next
etime=MilliSecs()-stime
Print etime
stime=MilliSecs()
For Local i:Int = 1 To 1000000000
Select a
Case 2
Case 3
Case 4
Default
End Select
Next
etime=MilliSecs()-stime
Print etime
Don't know if this helps. I get about 25% faster on the If..EndIf with only one decision. When I nest the decisions 3 deep like this example the If..Endif is only 8% faster. I tried it nested 9 levels deep and If..EndIf is still faster.
--deleted, incorrect results due to forgetting to reset the counter--
That's a big difference on your setup. I ran the tests again and they returned similar results to what I got before. I'm using the P4 like in my sig and BMax v1.18. It must be dependant on the CPU.
With your 2 tests, the first one returned 2801 and the second returned 2677.
Hmmm. The plot thickens.
Duh. I forgot to reset the timer in the ELSEIF test, so those results were way off base.
I corrected that just now, and it returns the following on my system:
CASE: 3492
NESTED IF: 5146
ELSEIF: 5237
Local a:Int=1
Local stime:Int
Local etime:Int
stime=MilliSecs()
For Local i2:Int = 1 To 1000000000
Select a
Case 2
Case 3
Case 4
Default
End Select
Next
etime=MilliSecs()-stime
Print "CASE "+etime
stime=MilliSecs()
For Local i:Int = 1 To 1000000000
If a=2
Else
If a=3
Else
If a=4
Else
End If
End If
End If
Next
etime=MilliSecs()-stime
Print "Nested IF "+etime
stime=MilliSecs()
For Local i3:Int = 1 To 1000000000
If a=2
ElseIf a=3
ElseIf a=4
End If
Next
etime=MilliSecs()-stime
Print "ELSEIF "+etime
I was going to ask you whether you'd altered one of the loop counts without updating the other....
...but then sleep kicked in.
Anyway, my results:
CASE 2876
NESTED IF 2554
ELSEIF 2494
Still the reverse situation. IMHO I'd still use CASE anyway as I think it's a cleaner and more readable implementation.
Still the reverse situation. IMHO I'd still use CASE anyway as I think it's a cleaner and more readable implementation.
My thoughts exactly...
thx a lot guys for all your imput on this...
--Mike
found it!!!
i think... looks like it was my particle generator...
i was checking to a see if the particle life was =0 before i deleted it, but my algorithm for generating and aging the particles could sometimes result in the life being a fractional value... and it could conceivably never equal 0, but go right past it to -.025, or something like that...
sort of a programmed in memory leak :) i would say... boy, i can be so stooooopid sometimes...
but hey, if we had a proper debugger (not much chance of that now eh :) ) this might've made itself apparent much sooner (gatta blame the system right :) )...
i eventually had to put up a textline to see how many particles was being generated, and how many remained after the sub stopped and no new particles were being created (they should've all died off eventually)...
if your particle code is based on the ole 'party' generator code then you might wanna change the line that checks to see if p\ent=0 to if p\ent<1... just to be sure :)
***Added***
now after saying all that about Blitz needing a good debugger, i have to say, that on the other hand, i'm really impressed with how fast Blitz3D handles this stuff... man i must have a million loops spinning around in here, and before i tracked down the bug, the particle count went up to near 900 before i started noticing any slowdown... impressive thing, this Blitz3D...
Mark... if you're out there, i just wanna THANK you for creating this thing the way you did... and i'm sold on the next B'Max version for sure...
(now this is not an attempt to kiss up or try to get an advanced copy of B'Max3D... i mean, not unless you want to, that is :) )
--Mike