Tips to speed up and optimize code

General discussion on mikroPascal.
Post Reply
Author
Message
Lajko
Posts: 38
Joined: 07 Oct 2008 21:49
Location: Seattle, WA USA
Contact:

Tips to speed up and optimize code

#1 Post by Lajko » 01 Dec 2008 08:37

I've got a pipe organ controller that needs to process data streams of 128 bits about 60 to 100 tomes per second. Coming from a Delphi background and Z-80 assembly (I'm used to Z-80 registers for addressing memory, which the PIC does not have such registers), I could use pointers to optimize the code for maximum speed. Things like eliminating loops and repeating the code does a lot. I look at the assembler created to see what things made for fewer instructions.

I'm using the 18F4620 because I know the 16 bit instruction PICs result in fewer instructions for bank switching memory. (I have a number of large arrays to deal with the range of pipes for organs.)

Does anyone have tips for everyone that will result in faster execution even though the resulting code would be bigger. Speed improvement also would be a relative thing.

Like, I had a routine to convert a byte to hex characters. It was a loop going from 0 to 1 but by eliminating the loop and repeating the code twice doubled the speed of the routine due to the overhead for the loop and calculating subscripts being eliminated. However, a subroutine with a huge loop of 1 to 100 would result in less relative improvement in speed because the loop overhead is a minor part compared to the rest of the subroutine.

So, anyone else have some speed-up tips? maybe the mE guys can offer some tips since they know the compiler. Efficiency of the resulting code compiled for a PIC is different than Delphi compiled for Windows (and I am migrating many routines from my Delphi library to the PIC pascal).
Creator of world's leading bowling league software for over 25 years. Join a bowling league today!
www.cdesoftware.com

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Tips to speed up and optimize code

#2 Post by janni » 01 Dec 2008 13:19

Lajko wrote:Does anyone have tips for everyone that will result in faster execution even though the resulting code would be bigger. Speed improvement also would be a relative thing.
For large arrays, especially with sequencial access, it's usually more effective to use PIC's indirect addressing mechanism instead of Pascal pointers. Use of FSR0,1,2 together with respective POSTINC, POSTDEC, etc. speeds things up significantly.

Here's an example of copying part of a table to another one:

Code: Select all

  FSR0Ptr:=@Table1+Offset;
  FSR1Ptr:=@Table2;
  i:=0;
  while i<n do
   begin
    POSTINC1:=POSTINC0;
    inc(i);
   end;

xor
Posts: 5465
Joined: 18 May 2005 00:59
Location: NYC
Contact:

#3 Post by xor » 02 Dec 2008 03:26

A subject that Janni is quite good at with mE compilers... :D

This is a good time of the year to remind some of our fellow programmers how far optimizing a simple routine can go... :shock:

On its 4th Christmas now:
CHRISTMAS LIGHTS SPECTACULAR OPTIMIZATION CHALLENGE
[color=darkred][b]xor[/b][/color]
[url=http://circuit-ed.com]CircuitED -[/url]

Post Reply

Return to “mikroPascal General”