What is better in python, dictionary or mysql?

What will be faster? Query mysql to find out if some of the information I need is needed OR load a python dictionary with all the information and then check if there is any

If python is faster, then what is the best way to check if an identifier exists?

Im using python 2.4.3

Im looking for data marked with a square on the board, im looking for x & y. There is only one entry per square, the information does not change, and it needs to be reminded several times per second.

Thank!

<Passed / h2>

I developed it was python. I ran the code below and mysql did it in 0.0003 seconds, but python did it in 0.000006 seconds and mysql is much less than a search, and a test was run to see how the code will work in real life. Whoever had less CPU and RAM overhead, I will never know, but if speed is something that needs to be done, then python has done much better.

And thanks for your answers!

def speedtest():
 global search
 global data
 qb = time.time()
 search.execute("SELECT * FROM `nogo` where `1`='11' AND `2`='13&3'")
 qa = search.fetchall()
 print qa[0]
 qc =  time.time()
 print "mysql"
 print qb
 print qc
 print qc - qb

 data = {}
 for qa in range(15):
  data[qa] = {}
  for qb in range(300):
   data[qa][str(qb)] = 'nogo'
 qw = 5
 qe = '50'
 qb = time.time()
 print data[qw][qe]
 qc =  time.time()
 print "dictionary"
 print qb
 print qc
 print qc - qb
+3
source share
4 answers

Generally speaking, if you need information from a database, ask the database about what you need . MySQL (and other database engines) are designed to extract data as efficiently as possible.

, MySQL .

, Python, , - .

+3

Python , .

my_dict.has_key('foobar')

Bloom filters.

+1

, , python , : 1) , ( , python) 2) ( dict ).

0

, MySQL ( -, ), Python dict IO ( ). , (x_pos, y_pos) 55 x 55 ( 3000 , 55 ^ 2 3000).

>>> the_dict = { (x, y) : None for x in range(55) for y in range (55) }
>>> len(the_dict)
3025
>>> import random
>>> xs = [random.randrange(0,110) for _ in range(55)]
>>> ys = [random.randrange(0,110) for _ in range(55)]
>>> import timeit
>>> total_secs = timeit.timeit("for x,y in zip(xs, ys): (x,y) in the_dict",
    setup="from __main__ import xs, ys, the_dict", number=100000)
>>> each_secs = total_secs / 100000
>>> each_secs
1.1723998441142385e-05
>>> each_usecs = 1000000 * each_secs
>>> each_usecs
11.723998441142385
>>> usecs_per_lookup = each_usecs / (55*55)
>>> usecs_per_lookup
0.0038757019640140115

0.004 (!) - , , ;) 2.4, YMMV . , ints ( ( - ) , xor ). , , ( ). , , .

0

Source: https://habr.com/ru/post/1768855/


All Articles