Fastest way to disect byte into 2 parts?
T Chap
Posts: 4,223
I have a PCF8574 that reads some inputs from two other devices, 4 bits per device. The main program loop needs to examine each of the two devices separately and ignore the output of the other device. What is the fastest way to evaluate the two halves using Spin?
Will using longs as temp holders be faster than bytes?
Here is my idea so far.
or
There are other things in the main loop, so the faster this works the better.
Will using longs as temp holders be faster than bytes?
Here is my idea so far.
PUB main Repeat Readinputs PUB Readinputs ReadVariable := ReadTheI2CBus 'ReadTheI2CBus returns byte with 8 inputs DeviceA := ReadVariable << 4 'work with the higher 4 bits for DevA DeviceB := ReadVariable >> 4 'work with the lower 4 bits for DevB
or
PUB Readinputs ReadVariable := ReadTheI2CBus 'ReadTheI2CBus returns byte with 8 inputs DeviceA := ReadVariable & %00001111 DeviceB := ReadVariable & %11110000
There are other things in the main loop, so the faster this works the better.
Comments
I'd stick to something like:
DeviceA := DeviceB := ReadTheI2CBus
DeviceA &= %00001111
DeviceB >>= 4
DeviceA and DeviceB could be bytes or longs. It doesn't really matter from a speed perspective.
When optimizing a program for speed, you never start with an optimized version because you don't know ahead of time where the bottlenecks are. You have to get the program working correctly first, then instrument it to find out where the delays are. They're often not where you think they are. The general rule is that programs spend > 90% of their time in < 10% of the code.