Last active
April 4, 2016 13:58
-
-
Save kwikwag/aaf8ba1c89e425669c4b01db5f6956b2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
def is_int1(v): | |
return v.lstrip('+-').isdigit() | |
def is_int2(v): | |
try: | |
int(v) | |
return True | |
except ValueError: | |
return False | |
def is_int3(v): | |
try: | |
int(v) | |
return True | |
except: | |
return False | |
def is_int4(v): | |
try: | |
return isinstance(int(v), int) | |
except: | |
pass | |
return False | |
import timeit | |
if __name__ == '__main__': | |
number = 1000000 | |
for s in ["238764", "2", "sdkjhf", "4398265.238476"]: | |
for ver in range(4): | |
func = 'is_int%d' % (ver+1,) | |
print 'func=%s, input=%s, time=%f' % (func, s, timeit.timeit('%s("%s")' % (func, s), setup='from __main__ import %s' % (func,), number=number)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Running on my system, I get: