0

So essentially what I am trying to do is generate a vector with components that are each bound within a specific and relative max and a min. So what I have the program doing right now is looking through each element of several vectors, finding the max and min, and then generating a value within that range.The only issue is this piece of the program takes forever to run, since the vectors are very large.

Numba sped it up a bit but not enough to be practical. So im wondering if there is either a pre-existing function/library that can do this that is a lot more optimized, or a better way to do it overall.

@numba.jit(target='cpu', nopython=False, parallel=True)
def rand_Vector(size, keys):

    array = []
    
    for g in trange((size), desc='Vectors_Elements:'):
    
        min = (embeddings[keys[0]])[g]
        max = (embeddings[keys[0]])[g]
        

        for key in keys:
            item = (embeddings[key])[g]

            if(item < min):
                min = item
                
            if(item > max):
                max = item
                
        array.append(random.uniform(min, max))
    return array
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Brk1145
  • 46
  • 4
  • 3
    I suggest using numpy. Also, python has built in `max()` and `min()` functions so you don't need to write this logic yourself. They are still linear in time since they have to loop through the data, but you wriet much less code using them. – Code-Apprentice Aug 18 '20 at 16:24
  • 2
    1. Appending to a list is slow. Use a list comprehension if possible. 2. Iterating over arrays is slow. Figure out a way to use [SIMD operations](https://stackoverflow.com/questions/44944367/are-numpys-basic-operations-vectorized-i-e-do-they-use-simd-operations) to do what you need. – Pranav Hosangadi Aug 18 '20 at 16:25

0 Answers0