优化前后新老代码如下:
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): """ 获取项目代码行语言相关统计——新方法(提升效率) 应用多线程替代for循环 并发访问共享外部资源 :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): """ 获取项目代码行语言相关统计——老方法(耗时严重) 使用最基本的思路进行编程 双层for循环嵌套并且每层都包含耗时操作 :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): """ 测试方法 :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循环和内层for循环里均存在耗时操作:
1)git.get_project_members()
2)git.get_user_info(member_name, debug=False)
分两步来优化,先里后外或先外后里都行。用多线程替换for循环,并发共享外部资源,加锁避免写冲突。
测试结果通过,函数运行时间装饰器显示(单位秒):
get_projects_lang_code_lines execution took up time:1.85294
get_projects_lang_code_lines_old execution took up time:108.604177
速度提升了约58倍
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新动态
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]