SoftwarSerial library suggestion

Hi. First time poster - so please don’t shoot me down too violently!
I was looking through the SoftwareSerial.cpp code. I noticed the following:

void SoftwareSerial::print(uint8_t b)
{
  if (_baudRate == 0)
    return;
  int bitDelay = _bitPeriod - clockCyclesToMicroseconds(50); // a digitalWrite is about 50 cycles
  byte mask;
  digitalWrite(_transmitPin, LOW);
  delayMicroseconds(bitDelay);
  for (mask = 0x01; mask; mask <<= 1) {
    if (b & mask){ // choose bit
      digitalWrite(_transmitPin,HIGH); // send 1
    }
    else{
      digitalWrite(_transmitPin,LOW); // send 1
    }
    delayMicroseconds(bitDelay);
  }
  digitalWrite(_transmitPin, HIGH);
  delayMicroseconds(bitDelay);
}

My question is, wouldn’t this save a few cycles ? I’m finding the whole thing a bit slow, and am looking for some speed-ups.

void SoftwareSerial::print(uint8_t b)
{
  if (_baudRate == 0)
    return;
  int bitDelay = _bitPeriod - clockCyclesToMicroseconds(50); // a digitalWrite is about 50 cycles
  byte mask;
  digitalWrite(_transmitPin, LOW);
  delayMicroseconds(bitDelay);
  for (mask = 0x01; mask; mask <<= 1) {
      digitalWrite(_transmitPin,(b & mask));
     delayMicroseconds(bitDelay);
  }
  digitalWrite(_transmitPin, HIGH);
  delayMicroseconds(bitDelay);
}

Regards,
Marv

I guess it would depend on what the compiler does with it - it may optimize it the same way; if you wanted to really know, disassemble the object code created...

Faster speed doesn't make a lot of sense here since you need a precisely timed interval between bits. This is asynchronous communication and data communication is made possible only because both ends assume a fixed bitrate (baudrate).

If you speed up the sender side (the function quoted is responsible for shifting out the bits serially), communication would become unreliable until eventually the receiver could no longer make sense of incoming data.

digitalWrite(_transmitPin,(b & mask));
    delayMicroseconds(bitDelay);

Assume you've got a baud rate of 9600.
A single bit takes 104 microseconds to transmit. You've got ten bits to transmit.
The processor is running at approximately 62ns per instruction.
You do the arithmetic.
(and when you've done it, please go back to your original post, select "modify", highlight all the code, then click on the "#" code button)

Good post though - keep it up.