Many users need faster decimal print for logging data to SD cards. The big problem with Print is this line
return write(str);
A class derived from Print may have a fast write for strings but it is not uses since Print defines these members
size_t write(const char *str) {
if (str == NULL) return 0;
return write((const uint8_t *)str, strlen(str));
}
size_t Print::write(const uint8_t *buffer, size_t size)
{
size_t n = 0;
while (size--) {
n += write(*buffer++);
}
return n;
}
So printNumber uses the above write(const char *str) and the string is written character at a time.
I did a test with the new version of print. It is faster but not as much as one would expect.
I timed this loop
for (int i = 0; i < 20000; i++) {
file.println(i);
}
The result for the old print is:
Time 9.38 sec
File size 128.89 KB
Write 13.74 KB/sec
For the new print with Stimmer's optimization.
Time 6.00 sec
File size 128.89 KB
Write 21.50 KB/sec
For a simple printField() function with no optimized divide that uses SdFat's write(const char*)
Time 2.66 sec
File size 128.89 KB
Write 48.44 KB/sec
So the improvement in Print will not be fully realized as long as printNumber uses Print::write(const char*).
I believe there is a way to fix this in a class derived from Print since size_t Print::write(const char *str) is not pure virtual. Correct me if I am wrong.