none
Can I optimize this more? RRS feed

  • Question

  • Hi there guys!

    i wondered if there's a way to optimize my scrolling code more. import: CNG024

    i think i optimized it as much as possible, but im not sure...
    plz take a look.

    The controls are arrow keys for movement and the space bar for a test attack. but this is quite obvious as soon as you've seen the code^^
    Live for nothing, OR CODE FOR SOMETHING!
    Tuesday, October 27, 2009 7:17 PM

Answers

  • One thing I noticed - only because you wanted it optimized as much as possible
    ...
    Game_Width = 1920
    ...

    Sub PlayerCode
      ...
      Player_Left = Scroll_X+Game_Width/2-Player_Width/2 
      Player_Right = Scroll_X+Game_Width/2+Player_Width/2
      ...
    EndSub

    The compiler is spending time dividing Game_Width by 2 every iteration (loop).  This is unnecessary.
    Instead, you should create a new variable that is half of Game_Width and add that.

    The same can be done to Player_Width

    ...
    Game_Width = 1920
    Half_Game_Width = Game_Width / 2
    ...
    Player_Width = 80
    Half_Player_Width = Player_Width / 2
    ...

    Sub PlayerCode
      ...
      Player_Left = Scroll_X+Half_Game_Width-Half_Player_Width
      Player_Right = Scroll_X+Half_Game_Width+Half_Player_Width
      ...
    EndSub

    What normally took 12 opcodes in CIL, now only takes 6, per loop
    • Marked as answer by Dudeson Thursday, October 29, 2009 5:17 PM
    Wednesday, October 28, 2009 7:31 PM

All replies

  • can no one help here??
    Live for nothing, OR CODE FOR SOMETHING!
    Wednesday, October 28, 2009 5:27 PM
  • Your code looks efficient to me.  Remember to check the inside of loops most carefully, especially for SmallBasic commands that may be unnecessary - none here.
    Wednesday, October 28, 2009 7:20 PM
    Moderator
  • One thing I noticed - only because you wanted it optimized as much as possible
    ...
    Game_Width = 1920
    ...

    Sub PlayerCode
      ...
      Player_Left = Scroll_X+Game_Width/2-Player_Width/2 
      Player_Right = Scroll_X+Game_Width/2+Player_Width/2
      ...
    EndSub

    The compiler is spending time dividing Game_Width by 2 every iteration (loop).  This is unnecessary.
    Instead, you should create a new variable that is half of Game_Width and add that.

    The same can be done to Player_Width

    ...
    Game_Width = 1920
    Half_Game_Width = Game_Width / 2
    ...
    Player_Width = 80
    Half_Player_Width = Player_Width / 2
    ...

    Sub PlayerCode
      ...
      Player_Left = Scroll_X+Half_Game_Width-Half_Player_Width
      Player_Right = Scroll_X+Half_Game_Width+Half_Player_Width
      ...
    EndSub

    What normally took 12 opcodes in CIL, now only takes 6, per loop
    • Marked as answer by Dudeson Thursday, October 29, 2009 5:17 PM
    Wednesday, October 28, 2009 7:31 PM
  • ...
    Game_Width = 1920
    Half_Game_Width = Game_Width / 2
    ...
    Player_Width = 80
    Half_Player_Width = Player_Width / 2
    ...

    Sub PlayerCode
      ...
      Player_Left = Scroll_X+Half_Game_Width-Half_Player_Width
      Player_Right = Scroll_X+Half_Game_Width+Half_Player_Width
      ...
    EndSub

    oh cool! thx!

    but do calculations like this even suck a noticeable amount of performance??

    Live for nothing, OR CODE FOR SOMETHING!
    Thursday, October 29, 2009 5:18 PM
  • As usual - try it and see.

    I doubt this will be noticeable, but the principal of only doing something once if you can is good.  The main overheads seem to be calls to SmallBasic commands or working with large multi-dimensional arrays, and not probably basic arithmetic.
    Thursday, October 29, 2009 8:46 PM
    Moderator