How to increase the performance of a search with for

Asked

Viewed 54 times

0

Basically the program I did does this same procedure, but with more data. I need to increase search performance, of course this can be done with libraries if it’s better. No matter how you improve performance, as long as the data format remains.

import time
import random

numbers = []
searchs = []
search_range = 1

for i in range(0, 9999):
    numbers.append((int(random.random() * 100), int(random.random() * 100), int(random.random() * 100)))
    searchs.append((int(random.random() * 100), int(random.random() * 100), int(random.random() * 100)))

start = time.time()
for search in searchs:
    for number in numbers:
        if (search[0] > (number[0] - search_range) and search[0] < (number[0] + search_range) and
            search[1] > (number[1] - search_range) and search[1] < (number[1] + search_range) and
            search[2] > (number[2] - search_range) and search[2] < (number[2] + search_range)):
            print(number)
            break

print('Time: %.2f' % (time.time() - start))

Note: Remembering that only what I want is the performance of the search, the rest of the program was just an example.

  • It doesn’t solve the problem, but makes the code more pythonic: https://repl.it/repls/ClosedNormalMarmot, as far as comparisons/ranges are concerned, you can also use randint and you don’t need to make it whole

  • @Miguel I didn’t know that I could do it this way, I’m going to correct it. And you know some library that I search this way but with more performance ??

  • You will always have to compare each value of one list with each value of the other. That way I was able to reduce 2-3secs, it’s not relevant but it reduces a little

  • @Miguel I thought I was going to have a library for this kind of processing.

  • One more optimization: https://repl.it/repls/ClosedNormalMarmot . By avoiding internal print, and using a generator

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.