def get_cpu_percentage(function, *args, **dargs):
 child_pre = resource.getrusage(resource.RUSAGE_CHILDREN)
 self_pre = resource.getrusage(resource.RUSAGE_SELF)
 start = time.time()
 to_return = function(*args, **dargs)
 elapsed = (time.time() - start)
 self_post = resource.getrusage(resource.RUSAGE_SELF)
 child_post = resource.getrusage(resource.RUSAGE_CHILDREN)
 (s_user, s_system) = [(a - b) for (a, b) in zip(self_post, self_pre)[:2]]
 (c_user, c_system) = [(a - b) for (a, b) in zip(child_post, child_pre)[:2]]
 cpu_percent = ((((s_user + c_user) + s_system) + c_system) / elapsed)
 return (cpu_percent, to_return)

def get_cpu_percentage ( function , * args , ** dargs ) : child_pre = resource . getrusage ( resource . RUSAGE_CHILDREN ) self_pre = resource . getrusage ( resource . RUSAGE_SELF ) start = time . time ( ) to_return = function ( * args , ** dargs ) elapsed = ( time . time ( ) - start ) self_post = resource . getrusage ( resource . RUSAGE_SELF ) child_post = resource . getrusage ( resource . RUSAGE_CHILDREN ) ( s_user , s_system ) = [ ( a - b ) for ( a , b ) in zip ( self_post , self_pre ) [ : 2 ] ] ( c_user , c_system ) = [ ( a - b ) for ( a , b ) in zip ( child_post , child_pre ) [ : 2 ] ] cpu_percent = ( ( ( ( s_user + c_user ) + s_system ) + c_system ) / elapsed ) return ( cpu_percent , to_return )
returns a tuple containing the cpu% and return value from function call .