0

I'm trying to optimize small function returning the comparaison of two vectors element by element into an array on python 3, and I've been using the timeit module to see wat code runs the best.

I'm running into this message The slowest run took 4.84 times longer than the fastest. This could mean that an intermediate result is being cached., when trying one of my two functions.

It doesn't seem to me that anything could be cached on the function, and the other one I'm testing never display this message, so I assume that timeit is right when it tells me that something is getting cached, but I cannot understand why.

This is the function:

import numpy as np

TP_v=np.array(TP_vect)
SL_v=np.array(SL_vect)
size=49

def k():
    TP_arr = np.reshape(np.repeat(TP_v,size),(size,size),order='F')
    SL_arr = np.reshape(np.repeat(SL_v,size)[::-1],(size,size),order='A')
    return((SL_arr > TP_arr)*1 - (SL_arr < TP_arr)*1)

The other one I'm testing, that doesn't display the cache message:

def f():        
    ar1=[]
    for i in SL_v[::-1]:
        ar2=[]
        for j in TP_v:
            if j>i:
                ar2.append(-1)
            elif j==i:
                ar2.append(0)
            else:
                ar2.append(1)
        ar1.append(ar2)
    return ar1

It might be a stupid question, but I'm actually quite curious, because a 5 times difference in run time is pretty big, and I don't know if the result timeit gives me is accurate.

I'd really happy to know what causes this, if anyone knows.

Thanks

PS: If by any chance you think one of these function is not optimize, do not hesitate to tell me :)

Edit: I sort of understand what a result being cached mean, but I don't exactly knows what's the cause in my code, apparently using np.reshape is; I assume that I must take into account the slowest result?

Erlinska
  • 433
  • 5
  • 16
  • 1
    The last one you are appending to a list - This is not reallocating a block of memory which is what you are doing with reshape. That takes time to reallocate. – Chinny84 Sep 10 '17 at 03:05
  • Thanks for your answer, I didn't know that reshape was doing it this way, I imagine that I should take into account the slowest result, is there any better way than timeit to mesure the run time of functions in cases like this? – Erlinska Sep 10 '17 at 03:18

0 Answers0