2

I have an embedded processor that is running a trimmed down version of BASIC (Parallax BASIC Stamp). In a loop, I am writing 1024 values via the SPI bus.

In compiled languages, more speed can be gained by unrolling the loop (putting more statements into the loop, reducing the overhead to statement ratio). However, I am not sure about BASIC since it is an interpretive language and each statement is interpreted before it is executed.

Profiling is difficult since I have to find an available pin, write a pulse to it, then measure with an o'scope.

From a theory point of view, does loop unrolling in BASIC provide any speed benefits?

MPelletier
  • 16,256
  • 15
  • 86
  • 137
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • It's so difficult to find any information about basic isn't it! Seems every website has the word basic in it somewhere! – Rich Bradshaw May 10 '11 at 19:01
  • I would assume it does provide a benefit, if the code in the body of the loop is very lightweight. BTW, can you use an in-circuit-emulator? If so, you could step it and/or random-pause it. – Mike Dunlavey May 10 '11 at 19:25
  • @Mike Dunlavey: There is no ICE for the Parallax Stamp Board. All I have are print statements and the ability to write out signals. It may have a clock, but that is too much code to output the time (the Stamp Board doesn't have a lot of code or variable space). – Thomas Matthews May 10 '11 at 19:36
  • @Rich Bradshaw: Another issue is that there is no standard for BASIC. – Thomas Matthews May 10 '11 at 19:37
  • Do you need the speed benefit, or are you just trying to optimise without a reason? If you need the benefit, then you should be able to measure the difference in speed. – Rich Bradshaw May 10 '11 at 19:41
  • @Rich Bradshaw: I was hoping for an answer other than to measure it. – Thomas Matthews May 10 '11 at 19:47
  • Is there an emulator program, where you could run the program on a PC? There has to be a way to debug software for that board. – Mike Dunlavey May 11 '11 at 01:19

1 Answers1

1

In theory, loop unrolling reduces the amount of time spent on incrementing and comparison within the loop. By virtue of reducing the loop overhead time, there is a performance gain.

The amount of time gained may not be as significant on an interpreted program as a compiled program. There is an overhead of time required for the interpreter to fetch an instruction, interpret (build code) and execute the code for a statement. In order for the loop unrolling time savings to be significant, the time savings must be larger than this overhead.

Unlike microprocessors, interpreters may not be optimized for execution speed. Modern processors have high speed caches, branch prediction and look-ahead techniques. Some can even fetch new instructions into the cache as others are executed. Loop unrolling takes advantage of these features by reducing the number of jumps and making the execution more predictable. For compiled languages, this adds a significant savings (for large iterations). This performance time savings may not be applicable to most interpreters since they may not employ these features.

The best determination of performance improvement is through measurement. In my case, there has to be enough user complaint to justify the schedule hit for performing the measurement.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154