您好,登錄后才能下訂單哦!
這篇文章主要講解了提高python中for循環(huán)效率的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。
對(duì)于某個(gè)城市的出租車數(shù)據(jù),一天就有33210000條記錄,如何將每輛車的數(shù)據(jù)單獨(dú)拎出來(lái)放到一個(gè)專屬的文件中呢?
思路很簡(jiǎn)單:
就是循環(huán)33210000條記錄,將每輛車的數(shù)據(jù)搬運(yùn)到它該去的文件中。
但是對(duì)于3000多萬(wàn)條數(shù)據(jù),一個(gè)一個(gè)循環(huán)太消耗時(shí)間,我花了2個(gè)小時(shí)才搬運(yùn)了60萬(wàn)數(shù)據(jù),算算3000萬(wàn)我需要花費(fèi)100個(gè)小時(shí),也就需要4-5天。并且還需要保證這五天全天開(kāi)機(jī),不能出現(xiàn)卡機(jī)的事故。
因此,需要使用并行進(jìn)行for循環(huán)的技巧:
由于3000萬(wàn)數(shù)據(jù)放到csv中導(dǎo)致csv打不開(kāi),因此我就把一個(gè)csv通過(guò)split軟件將其切分成每份60萬(wàn),共53個(gè)csv。
我原來(lái)的思路是讀取文件夾,獲取由每一個(gè)60萬(wàn)的csv文件組成的列表,再分別對(duì)每一個(gè)60萬(wàn)的csv進(jìn)行處理。實(shí)質(zhì)上還是循環(huán)33210000次,并行for循環(huán)就是同時(shí)處理幾個(gè)60萬(wàn)的csv文件,就能成倍的減少時(shí)間消耗。
并行進(jìn)行for循環(huán)是受下面的方法啟發(fā):
我之前的做法類似這樣:
words = ['apple', 'bananan', 'cake', 'dumpling'] for word in words: print word
并行for循環(huán)類似這樣:
from multiprocessing.dummy import Pool as ThreadPool items = list() pool = ThreadPool() pool.map(process, items) pool.close() pool.join()
其中,process是進(jìn)行處理的函數(shù)
實(shí)例代碼如下:
# -*- coding: utf-8 -*- import time from multiprocessing.dummy import Pool as ThreadPool def process(item): print('正在并行for循環(huán)') print(item) time.sleep(5) items = ['apple', 'bananan', 'cake', 'dumpling'] pool = ThreadPool() pool.map(process, items) pool.close() pool.join()
補(bǔ)充知識(shí):Python3用多線程替代for循環(huán)提升程序運(yùn)行速度
優(yōu)化前后新老代碼如下:
from git_tools.git_tool import get_collect_projects, QQNews_Git from threading import Thread, Lock import datetime base_url = "http://git.xx.com" project_members_commits_lang_info = {} lock = Lock() threads = [] ''' Author:zenkilan ''' def count_time(func): def took_up_time(*args, **kwargs): start_time = datetime.datetime.now() ret = func(*args, **kwargs) end_time = datetime.datetime.now() took_up_time = (end_time - start_time).total_seconds() print(f"{func.__name__} execution took up time:{took_up_time}") return ret return took_up_time def get_project_member_lang_code_lines(git, member, begin_date, end_date): global project_members_commits_lang_info global lock member_name = member["username"] r = git.get_user_info(member_name) if not r["id"]: return user_commits_lang_info = git.get_commits_user_lang_diff_between(r["id"], begin_date, end_date) if len(user_commits_lang_info) == 0: return lock.acquire() project_members_commits_lang_info.setdefault(git.project, dict()) project_members_commits_lang_info[git.project][member_name] = user_commits_lang_info lock.release() def get_project_lang_code_lines(project, begin_date, end_date): global threads git = QQNews_Git(project[1], base_url, project[0]) project_members = git.get_project_members() if len(project_members) == 0: return for member in project_members: thread = Thread(target=get_project_member_lang_code_lines, args=(git, member, begin_date, end_date)) threads.append(thread) thread.start() @count_time def get_projects_lang_code_lines(begin_date, end_date): """ 獲取項(xiàng)目代碼行語(yǔ)言相關(guān)統(tǒng)計(jì)——新方法(提升效率) 應(yīng)用多線程替代for循環(huán) 并發(fā)訪問(wèn)共享外部資源 :return: """ global project_members_commits_lang_info global threads for project in get_collect_projects(): thread = Thread(target=get_project_lang_code_lines, args=(project, begin_date, end_date)) threads.append(thread) thread.start() @count_time def get_projects_lang_code_lines_old(begin_date, end_date): """ 獲取項(xiàng)目代碼行語(yǔ)言相關(guān)統(tǒng)計(jì)——老方法(耗時(shí)嚴(yán)重) 使用最基本的思路進(jìn)行編程 雙層for循環(huán)嵌套并且每層都包含耗時(shí)操作 :return: """ project_members_commits_lang_info = {} for project in get_collect_projects(): git = QQNews_Git(project[1], base_url, project[0]) project_members = git.get_project_members() user_commits_lang_info_dict = {} if len(project_members) == 0: continue for member in project_members: member_name = member["username"] r = git.get_user_info(member_name, debug=False) if not r["id"]: continue try: user_commits_lang_info = git.get_commits_user_lang_diff_between(r["id"], begin_date, end_date) if len(user_commits_lang_info) == 0: continue user_commits_lang_info_dict[member_name] = user_commits_lang_info project_members_commits_lang_info[git.project] = user_commits_lang_info_dict except: pass return project_members_commits_lang_info def test_results_equal(resultA, resultB): """ 測(cè)試方法 :param resultA: :param resultB: :return: """ print(resultA) print(resultB) assert len(str(resultA)) == len(str(resultB)) if __name__ == '__main__': from git_tools.config import begin_date, end_date get_projects_lang_code_lines(begin_date, end_date) for t in threads: t.join() old_result = get_projects_lang_code_lines_old(begin_date, end_date) test_results_equal(old_result, project_members_commits_lang_info)
老方法里外層for循環(huán)和內(nèi)層for循環(huán)里均存在耗時(shí)操作:
1)git.get_project_members()
2)git.get_user_info(member_name, debug=False)
分兩步來(lái)優(yōu)化,先里后外或先外后里都行。用多線程替換for循環(huán),并發(fā)共享外部資源,加鎖避免寫(xiě)沖突。
測(cè)試結(jié)果通過(guò),函數(shù)運(yùn)行時(shí)間裝飾器顯示(單位秒):
get_projects_lang_code_lines execution took up time:1.85294
get_projects_lang_code_lines_old execution took up time:108.604177
速度提升了約58倍
看完上述內(nèi)容,是不是對(duì)提高python中for循環(huán)效率的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。