PDA

View Full Version : Subroutine or repeated code, which is more memory efficient?



CuriousOne
- 5th June 2014, 16:11
I'm writing a program, where I have to use



sound xma,[125,10]
pause 100


about 30 times. Should I make this as subroutine, will it be more space efficient, or compiler takes care of that?

Demon
- 5th June 2014, 18:12
Henrik could give more detailed answer, in the meantime...

A loop of 2 or 3 is structured but not always smaller in code (especially when short like yours).

But 30 times, I'd go with a loop.

You can always compile both ways and check code size (Microcode Studio posts it at the bottom).


Robert

HenrikOlsson
- 5th June 2014, 19:26
Hi,
What happend to simply trying a couple of things yourself and see instead of just asking. I just tried it here and it took less than five minutes to try both ways and write this reply.

Same routine repeated 30 times: 458 bytes
Subroutine and 30 GOSUBs: 201 bytes

This was compiled for a 16F877A whcih is the device you had questions about the other day. And no, the compiler doesn't decide for you - it's up to you as the programmer.

/Henrik.

Demon
- 5th June 2014, 19:28
YAY! I guessed right!

CuriousOne
- 5th June 2014, 22:38
Thanks!

I've tried to look at microcode studio displayed code size, but it does not seems to be in relation with actual code - I've added say 20 lines of new code, but sometimes size gets increased by only 1-2 words, sometimes - 200.