Ps991:
So.....um.....Im not going to say itPs991: 105192 uS per 1000 calls
RayLivingston: 49336 uS per 1000 calls
can you post your test sketch please?
Ps991:
So.....um.....Im not going to say itPs991: 105192 uS per 1000 calls
RayLivingston: 49336 uS per 1000 calls
can you post your test sketch please?
Already did
//I have no idea why this does not work, displays 9 0 8 7
because thousands is an unsigned long and the code uses %d for it in sprintf, should be %ld
Hmm, feel a little bit stupid for not catching that, I just noticed that the hundreds wasnt working correctly and when I added all %ld it failed...totalling forgot how it actually reads/stores those values...
A variation to have more choices: splitDigits into an array by repeated subtraction
long t;
uint8_t digits[10];
uint32_t x = 9876;
uint16_t y = 9876;
void setup()
{
Serial.begin(115200);
t = micros();
for (int i = 0; i < 1000; ++i)
{
splitDigits10(x, digits);
}
t = micros() - t;
Serial.print("splitDigits10: ");
Serial.print(t);
Serial.print(" uS per 1000 calls\n");
for (int i = 0; i < 10; i++) {
Serial.print(digits[i]);
Serial.println(' ');
}
Serial.println();
t = micros();
for (int i = 0; i < 1000; ++i)
{
splitDigits4(y, digits);
}
t = micros() - t;
Serial.print("splitDigits4: ");
Serial.print(t);
Serial.print(" uS per 1000 calls\n");
for (int i = 0; i < 4; i++) {
Serial.print(digits[i]);
Serial.println(' ');
}
Serial.println();
}
void loop() {}
void splitDigits10(uint32_t in, uint8_t ar[])
{
for (uint8_t i = 0; i < 10; i++) ar[i] = 0;
while (in >= 1000000000L) {
in -= 1000000000L;
ar[0]++;
}
while (in >= 100000000L) {
in -= 100000000L;
ar[1]++;
}
while (in >= 10000000L) {
in -= 10000000L;
ar[2]++;
}
while (in >= 1000000L) {
in -= 1000000L;
ar[3]++;
}
while (in >= 100000L) {
in -= 100000L;
ar[4]++;
}
while (in >= 10000L) {
in -= 10000L;
ar[5]++;
}
while (in >= 1000L) {
in -= 1000L;
ar[6]++;
}
while (in >= 100L) {
in -= 100L;
ar[7]++;
}
while (in >= 10L) {
in -= 10L;
ar[8]++;
}
ar[9] = in;
}
void splitDigits4(uint16_t in, uint8_t ar[])
{
for (uint8_t i = 0; i < 4; i++) ar[i] = 0;
while (in >= 1000) {
in -= 1000;
ar[0]++;
}
while (in >= 100) {
in -= 100;
ar[1]++;
}
while (in >= 10) {
in -= 10;
ar[2]++;
}
ar[3] = in;
}
splitDigits10: 34396 uS per 1000 calls
0
0
0
0
0
0
9
8
7
6
splitDigits4: 21796 uS per 1000 calls
9
8
7
6
splitDigits4() uses uint16_t so not as versatile as divmod() which does uint32_t
but it challenges its speed without assembly
(note: there is also a optimized divmod10 for 16 bits values, see thread mentioned above)
Problem with that code is that it is optimized by the compiler, declare x and y as volatile and you get different timings...
Think about it, if splitDigit10 takes 34.396uS (~137 cycles), thats too short.
Each iteration compares a LONG with the input (4 clock cycles) then uses a subtraction on a LONG (4 clock cycles), then adds 1 to a variable (1 clock cycle)
Considering you have a 9 and an 8 and a 7 and a 6 in there, you are looking at 99 + 89 + 79 + 69 + 4*6 (the 0s) = 294 cycles. Although I am probably wrong with this logic...
splitDigits10: 406240 uS per 1000 calls
0
0
0
0
0
0
9
8
7
6
splitDigits4: 66404 uS per 1000 calls
9
8
7
6
fastest variation I found sofar, splitDigits4() "binary search", trading size for speed
long t;
uint8_t digits[10];
void setup()
{
Serial.begin(115200);
t = micros();
for (int i = 0; i < 10000; ++i)
{
splitDigits10(i, digits); // check all 10000 possibilities
}
t = micros() - t;
Serial.print("splitDigits10: ");
Serial.print(t);
Serial.print(" uS per 10000 calls\n");
for (int i = 0; i < 10; i++) {
Serial.print(digits[i]);
Serial.print(' ');
}
Serial.println();
t = micros();
for (uint16_t i = 0; i < 10000; ++i)
{
splitDigits4(i, digits); // check all 10000 possibilities
}
t = micros() - t;
Serial.print("splitDigits4: ");
Serial.print(t);
Serial.print(" uS per 10000 calls\n");
splitDigits4(9876, digits);
for (int i = 0; i < 4; i++) {
Serial.print(digits[i]);
Serial.print(' ');
}
Serial.println();
}
void loop() {}
void splitDigits10(uint32_t in, uint8_t ar[])
{
for (uint8_t i = 0; i < 10; i++) ar[i] = 0;
while (in >= 1000000000L) {
in -= 1000000000L;
ar[0]++;
}
while (in >= 100000000L) {
in -= 100000000L;
ar[1]++;
}
while (in >= 10000000L) {
in -= 10000000L;
ar[2]++;
}
while (in >= 1000000L) {
in -= 1000000L;
ar[3]++;
}
while (in >= 100000L) {
in -= 100000L;
ar[4]++;
}
while (in >= 10000L) {
in -= 10000L;
ar[5]++;
}
while (in >= 1000L) {
in -= 1000L;
ar[6]++;
}
while (in >= 100L) {
in -= 100L;
ar[7]++;
}
while (in >= 10L) {
in -= 10L;
ar[8]++;
}
ar[9] = in;
}
// splitDigits4: 67884 uS per 10000 calls
void splitDigits4(uint16_t in, uint8_t ar[])
{
ar[0] = ar[1] = ar[2] = 0;
if (in >= 8000) {
in -= 8000;
ar[0] += 8;
}
if (in >= 4000) {
in -= 4000;
ar[0] += 4;
}
if (in >= 2000) {
in -= 2000;
ar[0] += 2;
}
if (in >= 1000) {
in -= 1000;
ar[0]++;
}
if (in >= 800) {
in -= 800;
ar[1] += 8;
}
if (in >= 400) {
in -= 400;
ar[1] += 4;
}
if (in >= 200) {
in -= 200;
ar[1] += 2;
}
if (in >= 100) {
in -= 100;
ar[1]++;
}
if (in >= 80) {
in -= 80;
ar[2] += 8;
}
if (in >= 40) {
in -= 40;
ar[2] += 4;
}
if (in >= 20) {
in -= 20;
ar[2] += 2;
}
if (in >= 10) {
in -= 10;
ar[2]++;
}
ar[3] = in;
}
/*
void splitDigits4(uint16_t in, uint8_t ar[])
{
ar[0] = ar[1] = ar[2] = 0;
while (in >= 1000) {
in -= 1000;
ar[0]++;
}
while (in >= 100) {
in -= 100;
ar[1]++;
}
while (in >= 10) {
in -= 10;
ar[2]++;
}
ar[3] = in;
}
*/
splitDigits10: 237372 uS per 10000 calls // note all possible 4 digit values tested (non volatile!)
0 0 0 0 0 0 9 9 9 9
splitDigits4: 69144 uS per 10000 calls // idem
9 8 7 6
updated to test all possible values.
Yeah compiler optimizes the timing quite a bit.
better to 2 calls and 1 call and check the diff ==> ~63300 for 10K calls
that is without the loop overhead
t = micros();
for (uint16_t i = 0; i < 10000; ++i)
{
splitDigits4(i, digits); // check all 10000 possibilities
splitDigits4(i, digits); // check all 10000 possibilities
}
t = micros() - t;
Serial.print("2 splitDigits4: ");
Serial.print(t);
Serial.print(" uS per 10000 calls\n");
t = micros();
for (uint16_t i = 0; i < 10000; ++i)
{
splitDigits4(i, digits); // check all 10000 possibilities
}
t = micros() - t;
Serial.print("1 splitDigits4: ");
Serial.print(t);
Serial.print(" uS per 10000 calls\n");
Add an extra uint8_t halfway to optimize the last subtractions
void splitDigits4(uint16_t in, uint8_t ar[])
{
ar[0] = ar[1] = ar[2] = 0;
if (in >= 8000) {
in -= 8000;
ar[0] += 8;
}
if (in >= 4000) {
in -= 4000;
ar[0] += 4;
}
if (in >= 2000) {
in -= 2000;
ar[0] += 2;
}
if (in >= 1000) {
in -= 1000;
ar[0]++;
}
if (in >= 800) {
in -= 800;
ar[1] += 8;
}
if (in >= 400) {
in -= 400;
ar[1] += 4;
}
if (in >= 200) {
in -= 200;
ar[1] += 2;
}
uint8_t in2 = in;
if (in2 >= 100) {
in2 -= 100;
ar[1]++;
}
if (in2 >= 80) {
in2 -= 80;
ar[2] += 8;
}
if (in2 >= 40) {
in2 -= 40;
ar[2] += 4;
}
if (in2 >= 20) {
in2 -= 20;
ar[2] += 2;
}
if (in2 >= 10) {
in2 -= 10;
ar[2]++;
}
ar[3] = in2;
}
splitDigits10: 237372 uS per 10000 calls
0 0 0 0 0 0 9 9 9 9
2 splitDigits4: 125360 uS per 10000 calls
1 splitDigits4: 65592 uS per 10000 calls
9 8 7 6
125360 - 65592 = 59768 uS per 10K calls
// enough played bedtime ![]()
OK, one more time, replace reference with local vars and skip one test sometimes.
void splitDigits4(uint16_t in, uint8_t ar[])
{
uint8_t d = 0, h = 0, t = 0, o;
if (in >= 8000) {
in -= 8000;
d = 8;
}
else if (in >= 4000) {
in -= 4000;
d = 4;
}
if (in >= 2000) {
in -= 2000;
d += 2;
}
if (in >= 1000) {
in -= 1000;
d++;
}
if (in >= 800) {
in -= 800;
h = 8;
}
else if (in >= 400) {
in -= 400;
h = 4;
}
if (in >= 200) {
in -= 200;
h += 2;
}
o = in;
if (o >= 100) {
o -= 100;
h++;
}
if (o >= 80) {
o -= 80;
t = 8;
}
else if (o >= 40) {
o -= 40;
t = 4;
}
if (o >= 20) {
o -= 20;
t += 2;
}
if (o >= 10) {
o -= 10;
t++;
}
ar[0] = d;
ar[1] = h;
ar[2] = t;
ar[3] = o;
}
splitDigits10: 237372 uS per 10000 calls
0 0 0 0 0 0 9 9 9 9
2 splitDigits4: 106240 uS per 10000 calls
1 splitDigits4: 56036 uS per 10000 calls
9 8 7 6
50204 uS / 10K calls
final one
//
// FILE: splitDigits4.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// PURPOSE: performace demo 4 segment display
// DATE: 2015-01-19
// URL: http://forum.arduino.cc/index.php?topic=293183.new#new
//
// Released to the public domain
//
long t;
uint8_t digits[10];
void setup()
{
Serial.begin(115200);
t = micros();
for (int i = 0; i < 10000; ++i)
{
splitDigits10(i, digits); // check all 10000 possibilities
}
t = micros() - t;
Serial.print("splitDigits10: ");
Serial.print(t);
Serial.print(" uS per 10000 calls\n");
for (int i = 0; i < 10; i++) {
Serial.print(digits[i]);
Serial.print(' ');
}
Serial.println();
t = micros();
for (uint16_t i = 0; i < 10000; ++i)
{
splitDigits4(i, digits); // check all 10000 possibilities
splitDigits4(i, digits); // check all 10000 possibilities
}
t = micros() - t;
Serial.print("2 splitDigits4: ");
Serial.print(t);
Serial.print(" uS per 10000 calls\n");
t = micros();
for (uint16_t i = 0; i < 10000; ++i)
{
splitDigits4(i, digits); // check all 10000 possibilities
}
t = micros() - t;
Serial.print("1 splitDigits4: ");
Serial.print(t);
Serial.print(" uS per 10000 calls\n");
splitDigits4(9876, digits);
for (int i = 0; i < 4; i++) {
Serial.print(digits[i]);
Serial.print(' ');
}
Serial.println();
}
void loop() {}
void splitDigits10(uint32_t in, uint8_t ar[])
{
for (uint8_t i = 0; i < 10; i++) ar[i] = 0;
while (in >= 1000000000L) {
in -= 1000000000L;
ar[0]++;
}
while (in >= 100000000L) {
in -= 100000000L;
ar[1]++;
}
while (in >= 10000000L) {
in -= 10000000L;
ar[2]++;
}
while (in >= 1000000L) {
in -= 1000000L;
ar[3]++;
}
while (in >= 100000L) {
in -= 100000L;
ar[4]++;
}
while (in >= 10000L) {
in -= 10000L;
ar[5]++;
}
while (in >= 1000L) {
in -= 1000L;
ar[6]++;
}
while (in >= 100L) {
in -= 100L;
ar[7]++;
}
while (in >= 10L) {
in -= 10L;
ar[8]++;
}
ar[9] = in;
}
void splitDigits4(uint16_t in, uint8_t ar[])
{
uint8_t d = 0, h = 0, t = 0, o;
if (in >= 8000) {
in -= 8000;
d = 8;
}
else
{
if (in >= 4000) {
in -= 4000;
d = 4;
}
if (in >= 2000) {
in -= 2000;
d += 2;
}
}
if (in >= 1000) {
in -= 1000;
d++;
}
if (in >= 800) {
in -= 800;
h = 8;
}
else
{
if (in >= 400) {
in -= 400;
h = 4;
}
if (in >= 200) {
in -= 200;
h += 2;
}
}
o = in;
if (o >= 100) {
o -= 100;
h++;
}
if (o >= 80) {
o -= 80;
t = 8;
}
else
{
if (o >= 40) {
o -= 40;
t = 4;
}
if (o >= 20) {
o -= 20;
t += 2;
}
}
if (o >= 10) {
o -= 10;
t++;
}
ar[0] = d;
ar[1] = h;
ar[2] = t;
ar[3] = o;
}
/*
void splitDigits4(uint16_t in, uint8_t ar[])
{
ar[0] = ar[1] = ar[2] = 0;
while (in >= 1000) {
in -= 1000;
ar[0]++;
}
while (in >= 100) {
in -= 100;
ar[1]++;
}
while (in >= 10) {
in -= 10;
ar[2]++;
}
ar[3] = in;
}
*/
splitDigits10: 237372 uS per 10000 calls
0 0 0 0 0 0 9 9 9 9
2 splitDigits4: 103228 uS per 10000 calls
1 splitDigits4: 54528 uS per 10000 calls
9 8 7 6
Bravo, even with volatile input the timing barely changes, its a little less than 4x faster than the assembly language one I found and like 20x faster than the first solution I posted...efficiency is fun
Ps991:
Bravo, even with volatile input the timing barely changes, its a little less than 4x faster than the assembly language one I found and like 20x faster than the first solution I posted...efficiency is fun
didn't expect to come this far ![]()
note that splitDigits4() does only 16 bit while the divmod10() does 32 bit.
not measured but I expect divmod10() has a smaller footprint
there is also a functional difference,
by working from the back divmod10() knows the position of the extracted digit,
while splitDigits() needs to process the complete number (a variation could extract the nth digit)
yes optimizing is fun, especially when it works ![]()