Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

How to increase a function speed being calling 400 time in Python

I have a list named dfs. It contains 400 Pandas dataframes of size 700 rows x 400 columns.

I have a function like this:

def updateDataframe(i):
    global dfs
    df = dfs[i]

    df["abc"].iloc[-1] = "xyz"

    df["abc2"] = df["abc"].rolling(10).mean()

    ........ #More pandas operations like this

    dfs[i] = df



for i in range(len(dfs)):
    updateDataframe(i)

Now, this loop takes 10 seconds to execute. I have tried python multi-processing, but it takes same time and somtimes even more.

Things I tried:

import multiprocessing.dummy as mp #Multi process Library, used for speeding up download
p=mp.Pool(8) #Define Number of Process to Use
p.map(updateDataframe,range(len(dfs))) # Call the Download Image funciton
p.close() #Close the multi threads
p.join()

Also tried this:

from multiprocessing import Process

if __name__ == "__main__":  # confirms that the code is under main function
    processes = []
    for i in range(len(dfs)):
        process = Process(target=updateDataframe, args=(i,))
        processes.append(process)
        processes.start()

    # complete the processes
    for i in range(len(processes)):
        processes[i].join()
question from:https://stackoverflow.com/questions/65932578/how-to-increase-a-function-speed-being-calling-400-time-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...