W
W
William Horn2019-11-17 18:44:19
Python
William Horn, 2019-11-17 18:44:19

How to speed up performance?

Good evening. There is a function that reads data from the database for the top and displays (VK bot).

def vkGetFirstName(id):
  return vkapi.users.get(user_id=id)[0]['first_name']
def vkGetLastName(id):
  return vkapi.users.get(user_id=id)[0]['last_name']

def e_top(chatId):
  table.execute("""SELECT user_id, drop_count FROM vkbottable WHERE chat_id = '{}' ORDER BY drop_count DESC""".format(chatId))
  text = "Топ еб**ноидов:\n"
  for i, row in enumerate(table):
    text += "{}. {} {} - {} раз\n".format(i+1, vkGetFirstName(int(row[0])), vkGetLastName(int(row[0])), row[1])
  return text

print(e_top(2000000004))

The problem is that this works for almost 13 seconds (12.6 on average). Can you speed up this piece of code?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
ScriptKiddo, 2019-11-17
@w_horn

text += "{}. {} {} - {} раз\n".format(i+1, vkGetFirstName(int(row[0])), vkGetLastName(int(row[0])), row[1])

Obviously, the name and surname do not change so often as to request them every time the top is displayed.
Write them to the database or pack requests in batches into the Execute method if you really need to request an IF every time

O
o5a, 2019-11-17
@o5a

users.get can take an array of user_ids at once, so you can immediately get the data of all users, and not pull each one by 1 request (all the more, do not pull 2 ​​requests for the first and last names separately).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question