Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Which is faster if or case ?

A typical single "if" statement will probably run nanoseconds faster than a case statement.

The question should be which is more efficient.

If you have a single test that is a simple 'yes' / 'no' answer, use an if statement.

If you have multiple answers, use a case statement.

It's similar to the difference between switches & variables

'if' returns true or false, that's it.

case allows you to stack a bunch of 'if' statements when testing the same variable / statement.

comprendo?
 

EOG

Member

That is not what I ment.

Where I heave
Code:
 

if a

 #a

elsif b

 #b

elseif

 #c

etc...

end

 
and
Code:
 

case s

when a

 #a

when b

 #b

when c

 #c

etc.

end

while

I know that inside if elery staitment will be checked untill it finds the right one so it can be:
a : false,b : false,c : false,d :true

and how does it works in case does it checks evey staitment till it finds right one or does it jums to the line(using label)
 
case statements check in order to my knowledge, but I've never used them in a scenerio where more than one of the things could be true. dunno if you are either, but I just wanted to point that out. if both A and C are true, the ifs would stop at A and execute A, ignoring C. I dunno what happens in the case statement.

but the most I've dealt with cases was in menus where you look for the menu index and depending on what was selected, you show a different screen.

If your menu has:

Equipment
Items
Status
etc...

your statement is like: (this may be slightly off because I don't have XP open right now to look at the exact syntax)

[rgss]case menu.index
when 0
#make status window active so you can select a character
when 1
#show items window
when 2
#technically the same code as 0... what happens when you select characters happens in another case statement
end
[/rgss]

so basically, depending on which index tests true, a different command can be excuted. in my opinion, the code is a lot neater than doing a bunch of elseifs, even if it would accomplish the same basic thing in the end.
 
You're not comparing apples to apples.

in "if a", a is a statement that returns true or false.

in "when a", a is the value of the variable 's'

your comparison should look like...

Code:
 

if s == 0

  # do this

elseif s == 1

  # do this

elseif s == 2

  # do this

else

  # do the default action

end

 

compared to:

 

case s

  when 0

    # do this

  when 1

    # do this

  when 2

    # do this

  else

    # do the default action

end

 

in case, you can also use a range. "when 2..5", whereas with 'if' you would need
if s >= 2 && s <= 5.

With 'case' you are testing every expression to see if it is equal (===) to 's'

with 'if', each test can be a completely different comparison.

if @face == "ugly"
# flip her over
elseif @finger.is_in(nose)
# tell her your name is Treg
end

We could probably do a benchmark with a incrementing loop to see which is more efficient, but I expect the results would be so close, that it just wouldn't matter.
Your decision should be based on your application. neither is 'slow'.

Be Well
 

Atoa

Member

The main diference of an if and a case is that a case can set conditions only for one instance, with an if you can set diferent instances.

E.g.:
Code:
case @test

when 1

 #

when 2

 #

when 3

 #

end

Code:
if @test == 1

 #

elsif  @test == 2

 #

eslif @variable == 1 and @test == 3

 #

else

 #

end

 

With the case you would have more problems to create a condition with differents instance.

But with case you could add more contional values.
E.g
Code:
case @test

when 1,2,3,5,6,12,45,32

 #

when 7,8,14,18

 #

else

 #

end
Imagine the number of "or" you would need to add to make this comparision with an "if".

But in RGSS there's a lot of ways of doing *excatly the same thing* in diferents ways, but with no chang ein the effectiveness.

In your case, if you want to compare differents instances, only an if could help you. Now if it's the same instance with different values, a canse would be more useful.
 
interesting. Get rid of the rand, and just set loops to 100000, then your answers are in microseconds per loop.
I was getting around 0.03, 0.028... meaning the diff is around 2 nanoseconds per loop.

Dang these 'puters are fast!!! :scruff:

Have you run into a situation where nanoseconds makes a difference? or just curiosity?

Be Well
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top