圆月山庄资源网 Design By www.vgjia.com
优化前后新老代码如下:
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倍
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年11月02日
2024年11月02日
- 《暗喻幻想》顺风耳作用介绍
- 崔健1985-梦中的倾诉[再版][WAV+CUE]
- 黄子馨《追星Xin的恋人们2》HQ头版限量编号[WAV+CUE]
- 孟庭苇《情人的眼泪》开盘母带[低速原抓WAV+CUE]
- 孙露《谁为我停留HQCD》[低速原抓WAV+CUE][1.1G]
- 孙悦《时光音乐会》纯银CD[低速原抓WAV+CUE][1.1G]
- 任然《渐晚》[FLAC/分轨][72.32MB]
- 英雄联盟新英雄安蓓萨上线了吗 新英雄安蓓萨技能介绍
- 魔兽世界奥杜尔竞速赛什么时候开启 奥杜尔竞速赛开启时间介绍
- 无畏契约CGRS准星代码多少 CGRS准星代码分享一览
- 张靓颖.2012-倾听【少城时代】【WAV+CUE】
- 游鸿明.1999-五月的雪【大宇国际】【WAV+CUE】
- 曹方.2005-遇见我【钛友文化】【WAV+CUE】
- Unity6引擎上线:稳定性提升、CPU性能最高提升4倍
- 人皇Sky今日举行婚礼!电竞传奇步入新篇章