Search code examples
pythonpython-3.xcython

How to measure cython run time and compare with python code?


I was using time.time for measuring inside pyx file and printing it to check times, but I think its not correct times for running, it showed different times for same lines of code and times are changing in each run, also it shows different times for each of for loops but all are doing same job, 0.010 and 0.020, and sometimes shows 0 for that part while shows 0.010 and 0.020 for 3 other sections. Please help me how should I measure it correctly, couldn't find any good way to measure time in cython docs

for this part of code it shows those 2 times and it changes sometimes in each run :

    t4 = time.time()
    # print('T3 =', t4 - t3)
    for j in range(np.shape(im1)[1]):
        # sum_c1[j] = np.shape(im1)[0] - (np.sum(im1[:, j]))
        sum_c1[j] = np.shape(im1)[0] - (np.count_nonzero(im1[:, j]))
    tt3 = time.time()
    print('TT3 =', tt3 - t4)
    cdef int amc1 = np.argmax(sum_c1)  # argmax sum_c1
    tt4 = time.time()
    # print('TT4 =', tt4 - tt3)
    for j in range(np.shape(im2)[1]):
        # sum_c2[j] = np.shape(im2)[0] - (np.sum(im2[:, j]))
        sum_c2[j] = np.shape(im2)[0] - (np.count_nonzero(im2[:, j]))
    t5 = time.time()
    print('TT5 =', t5 - tt4)
    # print('T4 =', t5 - t4)
    ## find of max zeros in row
    for j in range(np.shape(im1)[0]):
        # sum_r1[j] = np.shape(im1)[1] - (np.sum(im1[j, :]))
        sum_r1[j] = np.shape(im1)[1] - (np.count_nonzero(im1[j, :]))
    tt1 = time.time()
    print('TT1 =', tt1 - t5)
    cdef int amr1 = np.argmax(sum_r1)  # argmax sum_r1
    tt2 = time.time()
    # print('TT2 =', tt2 - tt1)
    for j in range(np.shape(im2)[0]):
        # sum_r2[j] = np.shape(im2)[1] - (np.sum(im2[j, :]))
        sum_r2[j] = np.shape(im2)[1] - (np.count_nonzero(im2[j, :]))
    t6 = time.time()
    print('T5 =', t6 - t5)
('TT3 =', 0.020589590072631836)
('TT5 =', 0.011527061462402344)
('TT1 =', 0.0)
('T5 =', 0.009999990463256836)

-----------
('TT3 =', 0.0100250244140625)
('TT5 =', 0.00996851921081543)
('TT1 =', 0.01003265380859375)
('T5 =', 0.020001888275146484)

these are 2 different runs on same code


Solution

  • What about using perf_counter()?

    start = time.perf_counter()
    # your code
    print(time.perf_counter()-start)
    

    More details here: https://stackoverflow.com/a/52228375/3872144