Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34.1k
Closed
Description
Feature or enhancement
Proposal:
Replacing switch-case inhex_from_char with a lookup table makes pythonfloat.fromhex(...) run 4%-5% faster. Probably if we measurehex_from_char in isolation we will get a much greater % improvement.
Lines 1145 to 1208 inf621618
| staticint | |
| hex_from_char(charc) { | |
| intx; | |
| 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; | |
| } | |
| returnx; | |
| } |
Newhex_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,};staticinthex_from_char(unsignedcharc) {return_char_to_hex[c];}
Timing with oldhex_from_char:
$ ./python -m pyperf timeit --duplicate 100"float.fromhex('0x123456.ffffp10')".....................Mean +- std dev: 86.0 ns +- 0.9 nsWith newhex_from_char:
./python -m pyperf timeit --duplicate 100"float.fromhex('0x123456.ffffp10')".....................Mean +- std dev: 81.8 ns +- 1.0 nsI will create a new PR.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response