#!/usr/bin/env python
'''use to function test and performance test'''
import fpformat
import time
def testit(func, *nkwargs, **kwargs):
try:
retval = func(*nkwargs, **kwargs)
result = (True, retval)
except Exception, diag:
result = (False, str(diag))
return result
def timeit(func, *nkwargs, **kwargs):
start_time = time.time()
retval = func(*nkwargs, **kwargs)
use_time = time.time() - start_time
use_time_str = fpformat.fix(use_time, 3)
return (retval, use_time_str)
def test():
funcs = (int, long, float)
vals = (1234, 12.34, '1234', '12.34')
for eachFunc in funcs:
print '_'*20
for eachVal in vals:
retval = testit(eachFunc, eachVal)
if retval[0]:
print '%s(%s) = ' %(eachFunc.__name__, eachVal), retval[1]
else:
print '%s(%s) = ' %(eachFunc.__name__, eachVal), retval[1]
vals = (1234, 12.34)
for eachFunc in funcs:
print '_'*20
for eachVal in vals:
retval = timeit(eachFunc, eachVal)
print '%s(%s) = ' %(eachFunc.__name__, eachVal), retval[0], ' , use time: ', retval[1]
if __name__ == '__main__':
test()