Pages: [1]   Go Down
Author Topic: print.cpp improvements?  (Read 925 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

in print.cpp there are many functions that use int base

Code:
void Print::print(char c, int base)
{
  print((long) c, base);
}

think  uint8_t base  should be enough as bases above 255 are never used or

don't know the effect on sketches yet, => to investigate
« Last Edit: January 13, 2013, 12:30:36 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yep,  strips of 14 bytes of a sketch that uses print   (2006 =>1992 bytes)

also changed #digits to uint8_t for printing floats/double

Should also strip some bytes from all derived classes smiley
« Last Edit: January 13, 2013, 12:30:44 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Posted as improvement - https://github.com/arduino/Arduino/issues/1222 -
« Last Edit: January 13, 2013, 12:40:50 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Other things found:

replace the indexer for printnumber from unsigned long to a byte ==> 60 bytes less !
removed the expensive modulo operator  ==> 10 bytes less! ==> update did fail some tests
Code:
void Print::printNumber(unsigned long n, uint8_t base)
{
  unsigned char buf[8 * sizeof(long)]; // Assumes 8-bit chars.
  // unsigned long i = 0;
  uint8_t i = 0;

  if (n == 0) {
    print('0');
    return;
  }

  while (n > 0)
  {
    buf[i++] = n % base;  
    n /= base;
  }

  for (; i > 0; i--)
    print((char) (buf[i - 1] < 10 ?
      '0' + buf[i - 1] :
      'A' + buf[i - 1] - 10));
}

replace 2 calls with one ==> 12 bytes less !
Code:
void Print::println(void)
{
  write("\r\n");
  // print('\r');
  // print('\n');  
}
« Last Edit: January 13, 2013, 03:21:54 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 216
Posts: 13664
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

replace float division with float multiplication in printFloat ==> 208 bytes less
+ replaced an int by an uint8_t ==> 6 bytes less
Code:
void Print::printFloat(double number, uint8_t digits)
{
  // Handle negative numbers
  if (number < 0.0)
  {
     print('-');
     number = -number;
  }

  // Round correctly so that print(1.999, 2) prints as "2.00"
  double rounding = 0.5;
  for (uint8_t i=0; i<digits; ++i)
    rounding *= 0.1;
    // rounding /= 10.0;  // <<<<<<<<<<<<<< more expensive than multiplication * 0.1
  
  number += rounding;

  // Extract the integer part of the number and print it
  unsigned long int_part = (unsigned long)number;
  double remainder = number - (double)int_part;
  print(int_part);

  // Print the decimal point, but only if there are digits beyond
  if (digits > 0)
    print(".");

  // Extract digits from the remainder one at a time
  while (digits-- > 0)
  {
    remainder *= 10.0;
    uint8_t toPrint = uint8_t (remainder);   // <<<<<<<<<<<<<<<< for 1 digit a byte is enough...
    print(toPrint, DEC);
    remainder -= toPrint;
  }
}

Sofar, in total 310 300 bytes less by looking carefully to the datatypes and operations in the print.cpp file

updated some numbers after a failing test.
« Last Edit: January 13, 2013, 03:22:38 pm by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1]   Go Up
Jump to: