Fastest version of code

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Fastest version of code

Post by spamiam »

I am trying to do a comparison and it is done fairly frequently. I am wondering which of these versions is the fastest.

I am converting some C code to ZBasic.

The original C code is:

Code: Select all

if(data & 0x08) LCD_DATA3_PORT |= _BV(LCD_DATA3_PIN);

The ZBasic versions that I have thought of are:

Code: Select all

if cBool(data AND &h08) then call PutPin(_data_pins(3),ZXOutputHigh)	
if (data AND &h08) > 0 then call PutPin(_data_pins(3),ZXOutputHigh)
if (data AND &h08) = &h08 then call PutPin(_data_pins(3),ZXOutputHigh)
Which of these comparisons is fastest?

And the C code sets the data direction register first (to 1), and Port is set to 0 pull-up, then the conditional only needs to set the Port register to 1 if the bit in the data byte is set.

In the ZBasic code, I set the pins to zxOutputHigh which I assume requires writing to the Port and DDR registers. I need to do 3 writes to port registers in C. I think ZBasic would need to do it in 4. Is there a way to cut that down to 3, similar to C?
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Fastest version of code

Post by dkinzer »

spamiam wrote:Which of these comparisons is fastest?
The first two are equivalent, the third is slightly slower due to the need to load the constant 8 for the comparison.
spamiam wrote:In the ZBasic code, I set the pins to zxOutputHigh which I assume requires writing to the Port and DDR registers.
If the pins in question are already outputs, you can simply set the bits of the PORTC register to the desired state. The SetBits() routine and the Register.PORT() built-in will be helpful for this purpose.
- Don Kinzer
rosariote
Posts: 39
Joined: 22 July 2007, 10:12 AM

Post by rosariote »

Hi,
Also you can instead of using the command
if (data AND &h08) > 0 then call PutPin(_data_pins(3),ZXOutputHigh)

use command pinlow(pin number) or pinhigh(pin number) This is more faster than the call PutPin.

if (data AND &h08) > 0 then pinlow (3) If you want the pin low
if (data AND &h08) > 0 then pinhigh (3) if you want the pin high
spamiam
Posts: 739
Joined: 13 November 2005, 6:39 AM

Post by spamiam »

rosariote wrote:Hi,
..
use command pinlow(pin number) or pinhigh(pin number) This is more faster than the call PutPin.
..
I had seen these instructions, and I had not specifically thought I would KNOW the pin number at compile time, as required for this family of instructions, but I can write the program that way. Since, I actually will be hardwiring the pins, and if I were to change it, it is easy to change the programming.

Thanks for making me think about it in this way.

-Tony
rosariote
Posts: 39
Joined: 22 July 2007, 10:12 AM

Post by rosariote »

Tony.
what I do it is define the pin as a constant at the beginning of the program. It make it easy to change the pin if it need to.

you can define it as :
const _data_pins as_3 byte = 3
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

spamiam wrote:I had seen these instructions, and I had not specifically thought I would KNOW the pin number at compile time[...]
If the pins need to be determined at run time, you can compute the address of the PORT register and the bit mask for each of the pins. Then, setting a pin high or low is as simple as setting or clearing the corresponding bit of the byte at the address.
- Don Kinzer
Post Reply