mirror of
https://gitlab.nic.cz/labs/bird.git
synced 2024-11-17 08:38:42 +00:00
Lib: Optimized printing of numbers
One simple trick makes printing of numbers ~4x faster.
This commit is contained in:
parent
5c6572c422
commit
49be4a0329
17
lib/printf.c
17
lib/printf.c
@ -73,11 +73,26 @@ static char * number(char * str, u64 num, uint base, int size, int precision,
|
|||||||
i = 0;
|
i = 0;
|
||||||
if (num == 0)
|
if (num == 0)
|
||||||
tmp[i++]='0';
|
tmp[i++]='0';
|
||||||
else while (num != 0) {
|
else if (base == 10) {
|
||||||
|
/* Separate cases to have fixed divisors */
|
||||||
|
while (num != 0) {
|
||||||
|
uint res = num % 10;
|
||||||
|
num = num / 10;
|
||||||
|
tmp[i++] = digits[res];
|
||||||
|
}
|
||||||
|
} else if (base == 16) {
|
||||||
|
while (num != 0) {
|
||||||
|
uint res = num % 16;
|
||||||
|
num = num / 16;
|
||||||
|
tmp[i++] = digits[res];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
while (num != 0) {
|
||||||
uint res = num % base;
|
uint res = num % base;
|
||||||
num = num / base;
|
num = num / base;
|
||||||
tmp[i++] = digits[res];
|
tmp[i++] = digits[res];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (i > precision)
|
if (i > precision)
|
||||||
precision = i;
|
precision = i;
|
||||||
size -= precision;
|
size -= precision;
|
||||||
|
Loading…
Reference in New Issue
Block a user