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.

A very minor code efficiency question.

Basically, I have a couple of BGMs in the BGM folder that I've used countless times now, yet whose name I need to change due to typo in file name, change of songs, etc. So instead of going back and spend a week searching & editing the name in the editor, I just made some exceptions in the bgm_play method.

Code:
def bgm_play(bgm)
    @playing_bgm = bgm

    #E.g. this block vvv
    if bgm != nil and bgm.name == "Old song name"
      Audio.bgm_play("Audio/BGM/" + "New song name", bgm.volume, bgm.pitch)

    elsif bgm != nil and bgm.name != ""
      Audio.bgm_play("Audio/BGM/" + bgm.name, bgm.volume, bgm.pitch)
    else
      Audio.bgm_stop
    end
    Graphics.frame_reset
  end

My question is, if I add a few of those exceptions there, would it slow things down noticeably? I'm pretty sure that it doesn't since it's constant time, but seeing those in a method that's run so often feels a little intimidating, so I just wanted some reassurance. If it does, is there another way of doing this?

Many thanks for your time.
 

e

Sponsor

This is a simple jump. I'm not sure exactly how the Ruby VM will interpret it, but usually it shouldn't add much more load; simple if/else jumps are, I think, translated into relative JMP operations, and those usually take ~1 cycle on x86 processors. So no, making a simple if/elsif/else shouldn't slow things down too much.

What you might do, however, if you truly want to optimize this, is put your string constants into...well, constants, instead of literals. See, each time you create a new string literal (e.g. : "Old song name"), it will create a new String object, even if the content is the same. Instead, you might want to do something like :

Code:
OLD_SONG_NAME = "Old song name"
NEW_SONG_NAME = "New song name"
AUDIO_DIR = "Audio/BGM/"

def bgm_play(bgm)
   @playing_bgm = bgm

   if !(bgm.nil?) and bgm.name == OLD_SONG_NAME
      Audio.bgm_play(AUDIO_DIR + NEW_SONG_NAME, bgm.volume, bgm.pitch)
  elsif !(bgm.nil?) and !(bgm.name.empty?)
     Audio.bgm_name(AUDIO_DIR + bgm.name, bgm.volume, bgm.pitch)
  else
     Audio.bgm_stop
  end

  Graphics.frame_reset
end

Now, I understand that your "New song" and "Old song" examples were dummies (or so I assume); still, string literals like these are never a good idea if you can predict the string beforehand. Try to use constants, class variables or something else to hold these.
 

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