How To use isdigit in python
For single-char strings, Strings' .isdigit() method is about 7% slower than simply checking ```if string in "0123456789"```. Possible bug?
Videos
I was looking to optimize a program of mine that works on very, very large text files. I was fiddling around with various algorithms that perform a certain task on said data, one part of which is to convert characters to an integer;
which, of course, first has to check whether the character is an integer in the first place.
try/except was slow for my intents and purposes, so I went with the good old-fashioned if-elif-else and ran various time-measuring tests on some 2 GB of text.
My findings:
if c.isdigit(): is about 7% slower than if c in "0123456789"
This susprised me because I assumed that the algorithm behind STRING.isdigit() is essentially a for loop that goes through each character of the string to check if it's a member of "0123456789".
And since I'm trying out both time-tests on ONE-character strings only, my expectation was that the performance times would be equal.
Well... they're not, as pointed above.
Should this be considered a bug? Clearly, c.isdigit() isn't operating as efficiently as it should.