Execution time

Hey guys,

not sure if should be better under Hardware of Software, and also not sure if this topic was discussed before, but I can’t find it seaching the forum.

The topic is the execution time of a command.

Running this code:

imp.configure("Square Generator", [], []); hardware.pin1.configure(DIGITAL_OUT); while(1){ hardware.pin1.write(1); hardware.pin1.write(0); }

My logic analizer report the result in attachment. The excution time is 0.196mS. And this is also the delay I get if for example I set a pin to low and another pin to high.

@Hugo, can you confirm that the minimum execution time between two lines/commands is about 200 microsecs?

Thanks,

Dimitri

P.S. I’m running this on a imp002 with probes connected directly on the pins.

A very similar code sequence is discussed and optimised on http://devwiki.electricimp.com/doku.php?id=writingefficientsquirrel – following the advice there should increase the speed by about 10x.

In general, though, if you want to generate fast signal patterns using an imp, it goes faster (and is more controllable) if you use the fixed-frequency DAC or an SPI peripheral. The idea should be to avoid doing timing-critical things in Squirrel: although we aim to preserve (or improve) Squirrel run-time performance from release to release, we can’t guarantee it.

Peter

I while back (when I first started playing with NeoPixels) I was looking into this as well. Here are four different ways to write to a DIGITAL_OUT pin, and scope shots to show how long execution for each method takes (note most of the scope shots have different x-axis resolutions):

Using hardware.pin.write (~200 microseconds)
function trigger() { hardware.pin1.write(1); hardware.pin1.write(0); }

Assigning hardware.pin to a global variable(~60 microseconds)
`p <- hardware.pin1;

function trigger() {
p.write(1);
p.write(0);
}`

Assigning hardware.pin.write to a global variable (~15 microseconds)
`w <- hardware.pin1.write;

function trigger() {
w(1);
w(0);
}`

Assigning hardware.pin.write to a local variable (~12 microseconds)
function trigger() { local w = hardware.pin1.write(); w(1); w(0); }

Edit: Fixed imgur image links

So, the trade off would be that a global variable for hardware.pin.write would persistently use more memory (to hold the variable), but greatly increase response time at the microsecond level?

Yes, although as @peter mentioned, if timing is really important, you should be using SPI or UART to write those values.

Does w <- hardware.pin1.write work? I haven’t tried it, but I’d expect it to need
p <- hardware.pin1; w <- p.write.bindenv(p);

In the next release that slight awkwardness (of the two variables) will be fixed, and you’ll be able to write
w <- hardware.pin1.write.bindenv(hardware.pin1)

Peter

Oh, while you’ve got the scope out: Brandon reckons it goes faster if you do w(1); w(0); all on one line. 12us is only about 1,500 machine cycles, and it’s possible that eliminating the small amount of overhead of keeping track of which line is being executed (which it does so that it can report error messages by line-number), will cut that 1,500 down a detectable amount.

Peter

@Peter - you are correct (you need to assign the pin to a global variable, and then that variables write function to another). It’s been a while some I did those experiments.

I’ll try the one line thing later today!