Subroutine or repeated code, which is more memory efficient?


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2013
    Posts
    1,078

    Default Subroutine or repeated code, which is more memory efficient?

    I'm writing a program, where I have to use

    Code:
    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?

  2. #2
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,597


    Did you find this post helpful? Yes | No

    Default Re: Subroutine or repeated code, which is more memory efficient?

    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
    Last edited by Demon; - 5th June 2014 at 18:14. Reason: compiler will not generate a loop for you, just repeat the instructions.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Subroutine or repeated code, which is more memory efficient?

    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.

  4. #4
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,597


    Did you find this post helpful? Yes | No

    Default Re: Subroutine or repeated code, which is more memory efficient?

    YAY! I guessed right!

  5. #5
    Join Date
    Feb 2013
    Posts
    1,078


    Did you find this post helpful? Yes | No

    Default Re: Subroutine or repeated code, which is more memory efficient?

    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.

Similar Threads

  1. Stopping repeated messages
    By GatorGuy in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 20th October 2011, 19:40
  2. Making code more efficient, any ideas please?
    By BobEdge in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 26th July 2011, 13:28
  3. Need the code to write to a memory
    By Hamlet in forum General
    Replies: 0
    Last Post: - 20th August 2007, 00:22
  4. Embedding Variables into Code Memory
    By JoeSolar in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 20th October 2006, 17:58
  5. fatal error [301] out of memory with 4 lines of code????
    By George in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 27th October 2005, 21:26

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts