-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
Closed
Labels
performancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement
Description
Feature or enhancement
Proposal:
Replacing switch-case in hex_from_char
with a lookup table makes python float.fromhex(...)
run 4%-5% faster. Probably if we measure hex_from_char
in isolation we will get a much greater % improvement.
Lines 1145 to 1208 in f621618
static int | |
hex_from_char(char c) { | |
int x; | |
switch(c) { | |
case '0': | |
x = 0; | |
break; | |
case '1': | |
x = 1; | |
break; | |
case '2': | |
x = 2; | |
break; | |
case '3': | |
x = 3; | |
break; | |
case '4': | |
x = 4; | |
break; | |
case '5': | |
x = 5; | |
break; | |
case '6': | |
x = 6; | |
break; | |
case '7': | |
x = 7; | |
break; | |
case '8': | |
x = 8; | |
break; | |
case '9': | |
x = 9; | |
break; | |
case 'a': | |
case 'A': | |
x = 10; | |
break; | |
case 'b': | |
case 'B': | |
x = 11; | |
break; | |
case 'c': | |
case 'C': | |
x = 12; | |
break; | |
case 'd': | |
case 'D': | |
x = 13; | |
break; | |
case 'e': | |
case 'E': | |
x = 14; | |
break; | |
case 'f': | |
case 'F': | |
x = 15; | |
break; | |
default: | |
x = -1; | |
break; | |
} | |
return x; | |
} |
New hex_from_char
code:
int _char_to_hex[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
static int
hex_from_char(unsigned char c) {
return _char_to_hex[c];
}
Timing with old hex_from_char
:
$ ./python -m pyperf timeit --duplicate 100 "float.fromhex('0x123456.ffffp10')"
.....................
Mean +- std dev: 86.0 ns +- 0.9 ns
With new hex_from_char
:
./python -m pyperf timeit --duplicate 100 "float.fromhex('0x123456.ffffp10')"
.....................
Mean +- std dev: 81.8 ns +- 1.0 ns
I will create a new PR.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
Metadata
Metadata
Assignees
Labels
performancePerformance or resource usagePerformance or resource usagetype-featureA feature request or enhancementA feature request or enhancement