代码拉取完成,页面将自动刷新
import multiprocessing
import time
import psutil
import os
from TtypeThreeLevelClass import TtypeThreeLevelCalculator
input_param = {'fout':50,
'PF':1,
'Vout':220,
'Vdc':800,
'I_rms':250,
'Rgon_T1':4,
'Rgoff_T1':4,
'Rgon_T2':3.6,
'Rgoff_T2':7.5,
'fsw':16000.0,
'Th':80,
}
inter_param = {'Vref':400,
'Rgon_T1_test':4.0,
'Rgoff_T1_test':4.0,
'Rgon_T2_test':4.0,
'Rgoff_T2_test':7.5,
}
R_coefs = {'T1_on':[1.411, 2.933, -0.08713, 0.001509],
'T1_off':[9.102, 0.9719, -0.03199, 0.0006902],
'T2_on':[1.756, 1.39, 0.02054, -0.000799],
'T2_off':[7.31, 0.2901, 0.01963, -0.0005018],
'D5_rec':[2.445, -0.2397, 0.01179, -0.0002233],
'D7_rec':[6.536, -0.2067, 0.001243, 9.488e-6],
}
loss_coefs = {'Vce_Ic_T1':[0.503, 0.006089, -8.08e-6, 4.252e-9],
'EonT1':[2.333e-3, 0.03273e-3, 5.639e-8, -6.142e-11],
'EoffT1':[2.516e-3, 0.04527e-3, -4.365e-9, 3.245e-12],
'Vce_Ic_T2':[0.4584, 0.004152, -6.261e-6, 3.719e-9],
'EonT2':[2.05e-3, 0.02808e-3, -1.859e-8, 1.881e-11],
'EoffT2':[0.6683e-3, 0.03976e-3, 9.78e-9, 2.054e-11],
'Vf_IC_D1':[0.4909, 0.01108, -2.25e-5, 1.933e-8],
'Erec_D1':[0.8853e-3, 0.02825e-3, -3.389e-8, 1.659e-11],
'Vf_IC_D2':[0.4689, 0.006365, -1.157e-5, 7.907e-9],
'Erec_D2':[0.1696e-3, 0.008464e-3, -8.512e-10, 2.156e-12],
}
Rth = {'Rth_T1':0.144, 'Rth_T2':0.253, 'Rth_D1':0.213, 'Rth_D2':0.27}
rc_coefs = {'T1':[0.024, 0.001, 0.044, 0.01, 0.02, 0.1, 0.056, 10.0],
'T2':[0.024, 0.001, 0.044, 0.01, 0.02, 0.1, 0.056, 10.0],
'D1':[0.024, 0.001, 0.044, 0.01, 0.02, 0.1, 0.056, 10.0],
'D2':[0.024, 0.001, 0.044, 0.01, 0.02, 0.1, 0.056, 10.0],
}
# class ExampleClass:
# def __init__(self, param1, param2):
# self.param1 = param1
# self.param2 = param2
# self.data = [i * i for i in range(10000)]
# def compute(self, arg1, arg2, arg3):
# time.sleep(1) # 模拟一些工作
# return sum(self.data) + arg1 + arg2 + arg3
def monitor_resources():
pid = os.getpid()
p = psutil.Process(pid)
# 获取CPU使用率
cpu_usage = p.cpu_percent(interval=1)
# 获取内存使用情况
memory_info = p.memory_info()
# 获取内存使用率
memory_percent = p.memory_percent()
return cpu_usage, memory_info.rss / 1024 / 1024, memory_percent
def run_serial():
resources_usage = []
for _ in range(10):
start_time = time.time()
obj = TtypeThreeLevelCalculator(input_param, inter_param)
losses = obj.cal_loss(R_coefs, loss_coefs, Rth)
curve_img = obj.draw_curve(rc_coefs)
end_time = time.time()
cpu_usage, memory_usage, memory_percent = monitor_resources()
resources_usage.append((end_time - start_time, cpu_usage, memory_usage, memory_percent))
return resources_usage
def example_function(param1, param2, arg1, arg2, arg3, arg4):
obj = TtypeThreeLevelCalculator(param1, param2)
losses = obj.cal_loss(arg1, arg2, arg3)
curve_img = obj.draw_curve(arg4)
return losses
def run_parallel():
resources_usage = []
with multiprocessing.Pool(processes=10) as pool:
futures = [pool.apply_async(example_function, (input_param, inter_param, R_coefs, loss_coefs, Rth, rc_coefs)) for _ in range(10)]
for future in futures:
start_time = time.time()
result = future.get()
end_time = time.time()
cpu_usage, memory_usage, memory_percent = monitor_resources()
resources_usage.append((end_time - start_time, cpu_usage, memory_usage, memory_percent))
return resources_usage
if __name__ == "__main__":
print("Running serially...")
serial_resources_usage = run_serial()
for i, (time_used, cpu, memory, memory_percent) in enumerate(serial_resources_usage):
print(f"Run {i+1}: Time used: {time_used:.2f} s, CPU: {cpu}%, Memory: {memory:.2f} MB, Memory Percent: {memory_percent:.2f}%")
# print("\nRunning in parallel...")
# parallel_resources_usage = run_parallel()
# for i, (time_used, cpu, memory, memory_percent) in enumerate(parallel_resources_usage):
# print(f"Run {i+1}: Time used: {time_used:.2f} s, CPU: {cpu}%, Memory: {memory:.2f} MB, Memory Percent: {memory_percent:.2f}%")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。