Page 2 of 2

Re: Help with efficiency of code

Posted: 27 Nov 2010, 07:19
by muntoo
Why don't you use the awesome method of software development (or anything, for that matter) of breaking things down? First, create a new file, and create a map loading/drawing function to see if they work. No other parts of your program should be in that file: no sound, no input, no anything; just loading/drawing. If possible, get rid of the loading.

Test to see if it works, and build on top of it in this order:
  • Basic Drawing Function
  • Basic File Loading Function
  • Basic File Saving Function
  • Advanced Drawing Function
  • Advanced File Loading Function
  • Advanced File Saving Function
Finally, implement it in your original program.

The best way to improve your program is to rewrite it. I know it's a lot of work, but that's the best way. You probably learned how to make your program while you were writing it, so you'll probably write code faster and cleaner than you did before. Look at your program in a new perspective. How else can you build it?

For example:

Code: Select all

int x = 0;
while(1)
{
    ClearScreen();
    TextOut(0, 0, "ABCDE", 0);
    TextOut(0, 8, "FGHIJ", 0);
    TextOut(0, 16, "KLMNO", 0);
    NumOut(0, 24, x, 0);
    Wait(100);

    x++;

    if(x >= 100)
        break;
}
Can be turned into:

Code: Select all

ClearScreen();

string szTemp = "";
for(int line = 0; line < 3; line++)
{
    szTemp = "";
    for(char charidx = (5 * line) + 'A'; charidx < (5 * (line + 1) + 'A'); charidx++)
    {
        szTemp += FlattenVar(charidx);
    }
    TextOut(0, line * 8, szTemp, 0);
}

for(int x = 0; x < 100; x++)
{
    NumOut(0, 24, x, 0);
    Wait(100);
}
Neither is "bad", or "better". It depends on what you want. Small file size? Speed? Readability? Maintainability?
Try to strike a balance between all of them.

The first piece of code scores the following [IMHO]:
  • File size: Unknown (smaller, in this case)
  • Speed: 5/10
  • Readability: 8/10
  • Maintainability: 2/10
If you want a simple, easy to read piece of code that you're absolutely sure you will not change in the future, this should suit your needs.
With a few tweaks, the speed could actually score better than the second piece of code.

The second piece of code scores the following [IMHO]:
  • File size: Unknown (smaller, in the long run)
  • Speed: 6/10
  • Readability: 4/10
  • Maintainability: 9/10
If you wanted to display variations of "ABCD.....WXYZ" on the screen, the second is best.
For example, by just changing a few constants, you can make it display virtually endless variations:

Code: Select all

AB
CD
EF
GH
IJ
KL
MN
OP

Code: Select all

ABCDEFGH
IJKLMNOP
QRSTUVWX

Code: Select all

GHI
JKL
MNO
And so on...

I recommend you read Code Complete [be sure to get the 2nd Edition, the first is "outdated"], if you want to learn more, and have a better explanation than what I gave you. In fact, I'm still reading it [I've been on Chapter 2 for over an year... -_-' ].

Re: Help with efficiency of code

Posted: 30 Nov 2010, 17:43
by nxtboyiii
I was thinking of making a map, saving it as a .txt file, and reading it in the program.
Though....I don't know how. :(