Skip to content

Commit

Permalink
Add timing decorator that can be used in profiling
Browse files Browse the repository at this point in the history
  • Loading branch information
rocky committed Aug 15, 2021
1 parent 7571f4a commit b2885a0
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions mathics/core/util.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import re
import sys
from itertools import chain

FORMAT_RE = re.compile(r"\`(\d*)\`")

import time

# A small, simple timing tool
MIN_ELAPSE_REPORT = int(os.environ.get("MIN_ELAPSE_REPORT", "0"))


def timeit(method):
"""Add this as a decorator to time parts of the code.
For example:
@timit
def long_running_function():
...
"""

def timed(*args, **kw):
method_name = method.__name__
# print(f"{date.today()} {method_name} starts")
ts = time.time()
result = method(*args, **kw)
te = time.time()
elapsed = int((te - ts) * 1000)
if elapsed > MIN_ELAPSE_REPORT:
if "log_time" in kw:
name = kw.get("log_name", method.__name__.upper())
kw["log_time"][name] = elapsed
else:
print("%r %2.2f ms" % (method_name, elapsed))
# print(f"{date.today()} {method_name} ends")
return result

return timed


def interpolate_string(text, get_param) -> str:
index = [1]
Expand Down

0 comments on commit b2885a0

Please sign in to comment.