Skip to content

Instantly share code, notes, and snippets.

@Shellbye
Created October 11, 2016 07:16
Show Gist options
  • Select an option

  • Save Shellbye/b0f75371354b007f58c277228dd73dc4 to your computer and use it in GitHub Desktop.

Select an option

Save Shellbye/b0f75371354b007f58c277228dd73dc4 to your computer and use it in GitHub Desktop.
Test time usage of python code
def time_helper(pre, timer, name="-", is_last_one=False, start_time=None):
"""
:param pre: time.clock, last timestamp
:param timer: list, container of all record
:param name: str, help to know where the timer records
:param is_last_one: boolean, whether need to print result
:param start_time: time.clock, calculate to entire time range
:return: time.clock, for next call
>>>timer = []
>>>start_time = time.clock()
>>>time0 = time_helper(time0, timer, "1st search")
>>>time0 = time_helper(time0, timer, "1st search", True, start_time)
"""
t = time.clock()
# import inspect # if you need line number
# info = inspect.getframeinfo(inspect.stack()[1][0])
timer.append(
{
# "line_no": info.lineno,
"range": (t - pre) * 1000.0,
"name": name
}
)
if is_last_one:
total_time = (time.clock() - start_time) * 1000.0
for t in timer:
t['percentage'] = t['range'] / total_time
sorted_timer = sorted(timer, key=lambda k: k['percentage'], reverse=True)
for n in sorted_timer:
print n
print "-----------"
return t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment