-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathtimer.py
More file actions
58 lines (43 loc) · 1.17 KB
/
timer.py
File metadata and controls
58 lines (43 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Logger utility
"""
from functools import wraps
from time import perf_counter
from typing import Callable
from typing import Tuple
import numpy as np
def timer(func: Callable) -> Callable:
"""Decorator to time a function.
Args:
func: Function to time
Returns:
Function results and time (in seconds)
"""
@wraps(func)
def wrapper(*args, **kwargs):
start = perf_counter()
results = func(*args, **kwargs)
end = perf_counter()
run_time = end - start
return results, run_time
return wrapper
@timer
def fit_with_time(model, X_train: np.array, y_train: np.array) -> Tuple:
"""Returns trained model with the time
Args:
model: Model to test latency on
X_test: Input data
Returns:
Predicted values and time taken to predict it
"""
return model.fit(X_train, y_train)
@timer
def predict_with_time(model, X_test: np.array) -> Tuple[np.array]:
"""Returns model output with the time
Args:
model: Model to test latency on
X_test: Input data
Returns:
Predicted values and time taken to predict it
"""
return model.predict(X_test)