Skip to content

Instantly share code, notes, and snippets.

@kwikwag
Last active April 4, 2016 13:58
Show Gist options
  • Save kwikwag/aaf8ba1c89e425669c4b01db5f6956b2 to your computer and use it in GitHub Desktop.
Save kwikwag/aaf8ba1c89e425669c4b01db5f6956b2 to your computer and use it in GitHub Desktop.
#!/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))
@kwikwag
Copy link
Author

kwikwag commented Apr 4, 2016

Running on my system, I get:

func=is_int1, input=238764, time=0.243841
func=is_int2, input=238764, time=0.416296
func=is_int3, input=238764, time=0.417030
func=is_int4, input=238764, time=0.530820
func=is_int1, input=2, time=0.240771
func=is_int2, input=2, time=0.411695
func=is_int3, input=2, time=0.409882
func=is_int4, input=2, time=0.522021
func=is_int1, input=sdkjhf, time=0.241361
func=is_int2, input=sdkjhf, time=2.180622
func=is_int3, input=sdkjhf, time=1.839075
func=is_int4, input=sdkjhf, time=1.903009
func=is_int1, input=4398265.238476, time=0.244732
func=is_int2, input=4398265.238476, time=2.249253
func=is_int3, input=4398265.238476, time=1.884120
func=is_int4, input=4398265.238476, time=2.002077

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment