Skip to content

Commit 68c766d

Browse files
committed
FEAT: Adding timeit function
1 parent a4b4e76 commit 68c766d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

arrayfire/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
from .bcast import *
5454
from .index import *
5555
from .interop import *
56+
from .timer import *
5657

5758
# do not export default modules as part of arrayfire
5859
del ct

arrayfire/timer.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#######################################################
2+
# Copyright (c) 2015, ArrayFire
3+
# All rights reserved.
4+
#
5+
# This file is distributed under 3-clause BSD license.
6+
# The complete license agreement can be obtained at:
7+
# http://arrayfire.com/licenses/BSD-3-Clause
8+
########################################################
9+
"""
10+
Functions to time arrayfire functions
11+
"""
12+
13+
from .library import *
14+
from .device import (sync, eval)
15+
from time import time
16+
17+
def timeit(af_func, *args, min_iters = 10):
18+
"""
19+
Function to time arrayfire functions.
20+
21+
Parameters
22+
----------
23+
24+
af_func : arrayfire function
25+
26+
*args : arguments to `af_func`
27+
28+
min_iters : Minimum number of iterations to be run for `af_func`
29+
30+
Returns
31+
--------
32+
33+
t : Time in seconds
34+
"""
35+
36+
res = af_func(*args)
37+
eval(res)
38+
sync()
39+
40+
start = time()
41+
elapsed = 0
42+
num_iters = 0
43+
while elapsed < 1:
44+
for n in range(min_iters):
45+
res = af_func(*args)
46+
eval(res)
47+
sync()
48+
elapsed += time() - start
49+
num_iters += min_iters
50+
51+
return elapsed / num_iters

0 commit comments

Comments
 (0)