Как ускорить выполнение программы
Помогите пожалуйста! Подскажите как можно ускорить мою программу?
import zipfile
import os
import xlwings as xw
import numpy as np
import time
import pandas as pd
import urllib.request
book = xw.Book('bestdata.xlsx')
sheet1 = book.sheets[1]
sheet2 = book.sheets[2]
sheet3 = book.sheets[3]
def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]
def bestchange_info():
print('Begining...')
url = "http://api.bestchange.ru/info.zip"
urllib.request.urlretrieve(url, '/Users/Sergey/PycharmProjects/BestChange arbitrage/info.zip')
z = zipfile.ZipFile('info.zip', 'r')
z.extractall()
z.close()
need_curr = ['93', '82', '76', '99', '2', '8', '10', '16', '19', '23', '24', '26', '27', '32', '36', '43', '48', '61', '104', '110', '115', '124', '133', '134', '135', '137',
'138', '139', '140', '149', '160', '161', '162', '163', '168', '172', '173', '175', '177', '178', '179', '180', '181', '182', '184', '185', '189', '197',
'198', '201', '202', '203', '205', '206', '208', '210', '212', '213', '216', '217', '218', '220', '227', '228', '235']
datContent = open('bm_cy.dat').read()
dam = datContent.split(sep='\n')
df = pd.DataFrame(dam)
df = df[0].str.split(';', expand=True)
df.columns = ['ID-curr', 'number', 'name', 'symbol', '1', '2', '3']
df = df[['ID-curr', 'name', 'symbol']]
y = []
for i in need_curr:
for j in df[['ID-curr', 'name', 'symbol']].values.tolist():
if j[0] == i:
x = y + j
y = x
else:
continue
my_list = np.array(y)
my_list = np.split(my_list, 65)
df1 = pd.DataFrame(my_list)
df1.columns = ['ID-curr', 'name', 'symbol']
datCon = open('bm_exch.dat').read()
dem = datCon.split(sep='\n')
df_exch = pd.DataFrame(dem)
df_exch = df_exch[0].str.split(';', expand=True)
df_exch.columns = ['ID-ex', 'name', '1', '2', '3']
df_exch = df_exch[['ID-ex', 'name']]
df_exch = df_exch.set_index('ID-ex')
sheet2["A1"].value = df_exch
datCont = open('bm_rates.dat').read()
doom = datCont.split(sep='\n')
df_need = pd.DataFrame(doom)
df_need = df_need[0].str.split(';', expand=True)
df_need.columns = ['ID-Give', 'ID-Get', 'ID-Exch', 'curr-give', 'curr-get', 'reserv', 'otziv', '1', 'min-give', 'max-give', 'ID-City']
mac = df_need.loc[df_need['ID-Give'].isin(need_curr)]
musk = mac.loc[mac['ID-Get'].isin(need_curr)]
musk = musk.values.tolist()
for i in musk:
d = f'https://www.bestchange.ru/click.php?id={i[2]}'
i.append(d)
df3 = pd.DataFrame(musk)
df3.columns = ['ID-Give', 'ID-Get', 'ID-Exch', 'curr-give', 'curr-get', 'reserv', 'otziv', '1', 'min-give', 'max-give',
'ID-City', 'url']
m = df3.values.tolist()
t0 = time.time()
q = []
for j in m:
for l in df1.values.tolist():
if j[0] == l[0]:
j[0] = l[1]
q = q + j
ad = list(chunks(q, 12))
df2 = pd.DataFrame(ad)
df2.columns = ['ID-Give', 'ID-Get', 'ID-Exch', 'curr-give', 'curr-get', 'reserv', 'otziv', '1', 'min-give', 'max-give',
'ID-City', 'url']
mynewlist = df2.values.tolist()
g = []
for k in mynewlist:
for m in df1.values.tolist():
if k[1] == m[0]:
k[1] = m[1]
g = g + k
final = list(chunks(g, 12))
final_df = pd.DataFrame(final)
final_df.columns = ['ID-Give', 'ID-Get', 'ID-Exch', 'curr-give', 'curr-get', 'reserv', 'otziv', '1', 'min-give',
'max-give',
'ID-City', 'url']
final_df = final_df[['ID-Give', 'ID-Get', 'ID-Exch', 'curr-give', 'curr-get', 'otziv', 'min-give',
'max-give', 'url']]
final_df.set_index('ID-Give')
print(final_df)
sheet3["A1"].value = final_df.sort_values('ID-Give')
t1 = time.time() - t0
print(t1)
path = [
"/Users/Sergey/PycharmProjects/BestChange arbitrage/info.zip", "/Users/Sergey/PycharmProjects/BestChange arbitrage/bm_bcodes.dat",
"/Users/Sergey/PycharmProjects/BestChange arbitrage/bm_brates.dat", "/Users/Sergey/PycharmProjects/BestChange arbitrage/bm_cities.dat",
"/Users/Sergey/PycharmProjects/BestChange arbitrage/bm_cy.dat", "/Users/Sergey/PycharmProjects/BestChange arbitrage/bm_cycodes.dat",
"/Users/Sergey/PycharmProjects/BestChange arbitrage/bm_exch.dat", "/Users/Sergey/PycharmProjects/BestChange arbitrage/bm_info.dat",
"/Users/Sergey/PycharmProjects/BestChange arbitrage/bm_news.dat", "/Users/Sergey/PycharmProjects/BestChange arbitrage/bm_rates.dat",
"/Users/Sergey/PycharmProjects/BestChange arbitrage/bm_top.dat"
]
for i in path:
os.remove(i)
print("Done")
def main():
bestchange_info()
if __name__ == '__main__':
main()