Go Down

Topic: print.cpp improvements? (Read 1 time) previous topic - next topic

robtillaart

Jan 13, 2013, 05:46 pm Last Edit: Jan 13, 2013, 06:30 pm by robtillaart Reason: 1
in print.cpp there are many functions that use int base

Code: [Select]
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
Rob Tillaart

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

robtillaart

#1
Jan 13, 2013, 05:59 pm Last Edit: Jan 13, 2013, 06:30 pm by robtillaart Reason: 1
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 :)
Rob Tillaart

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

robtillaart

#2
Jan 13, 2013, 06:30 pm Last Edit: Jan 13, 2013, 06:40 pm by robtillaart Reason: 1
Posted as improvement - https://github.com/arduino/Arduino/issues/1222 -
Rob Tillaart

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

robtillaart

#3
Jan 13, 2013, 07:04 pm Last Edit: Jan 13, 2013, 09:21 pm by robtillaart Reason: 1
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: [Select]
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: [Select]
void Print::println(void)
{
 write("\r\n");
 // print('\r');
 // print('\n');  
}
Rob Tillaart

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

robtillaart

#4
Jan 13, 2013, 07:30 pm Last Edit: Jan 13, 2013, 09:22 pm by robtillaart Reason: 1
replace float division with float multiplication in printFloat ==> 208 bytes less
+ replaced an int by an uint8_t ==> 6 bytes less
Code: [Select]
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.
Rob Tillaart

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

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy