You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何计算仿真循环的整体运行时间?计时代码位置咨询

如何为Python仿真实验循环添加计时功能

你需要统计整个实验循环的运行时间,只需借助Python内置的time模块,将计时代码放在循环的开始前和结束后即可。以下是具体修改方案:

原实验代码

def run_experiment():
    from notears import utils
    # utils.set_random_seed(1) this line cannot be used to ensure different outcomes in each round of the loop

    n, d, s0, graph_type, sem_type = 1000, 20, 20, 'ER', 'gauss'
    B_true = utils.simulate_dag(d, s0, graph_type)
    W_true = utils.simulate_parameter(B_true)
    np.savetxt('W_true.csv', W_true, delimiter=',')

    X = utils.simulate_linear_sem(W_true, n, sem_type)
    np.savetxt('X.csv', X, delimiter=',')

    W_est = notears_linear(X, lambda1=0.1, loss_type='l2')
    assert utils.is_dag(W_est)
    np.savetxt('W_est.csv', W_est, delimiter=',')
    acc = utils.count_accuracy(B_true, W_est != 0)
    print(acc)

if __name__ == '__main__':
    num_experiments = 3

    for _ in range(num_experiments):
        run_experiment()

方案1:统计整个循环的总运行时间

在循环启动前记录起始时间,循环全部结束后记录结束时间,两者差值即为总耗时。同时补充原代码缺失的模块导入(避免重复导入影响效率):

import time
import numpy as np
from notears import utils, notears_linear

def run_experiment():
    # utils.set_random_seed(1) this line cannot be used to ensure different outcomes in each round of the loop

    n, d, s0, graph_type, sem_type = 1000, 20, 20, 'ER', 'gauss'
    B_true = utils.simulate_dag(d, s0, graph_type)
    W_true = utils.simulate_parameter(B_true)
    np.savetxt('W_true.csv', W_true, delimiter=',')

    X = utils.simulate_linear_sem(W_true, n, sem_type)
    np.savetxt('X.csv', X, delimiter=',')

    W_est = notears_linear(X, lambda1=0.1, loss_type='l2')
    assert utils.is_dag(W_est)
    np.savetxt('W_est.csv', W_est, delimiter=',')
    acc = utils.count_accuracy(B_true, W_est != 0)
    print(acc)

if __name__ == '__main__':
    num_experiments = 3
    
    # 记录整个循环的起始时间
    start_total = time.time()
    
    for _ in range(num_experiments):
        run_experiment()
    
    # 记录结束时间并计算总耗时
    end_total = time.time()
    total_duration = end_total - start_total
    print(f"\n整个实验循环总运行时间: {total_duration:.2f} 秒")

方案2:同时统计单轮实验耗时与总耗时

如果需要查看每次实验的单独耗时,可将计时逻辑放入循环内部:

import time
import numpy as np
from notears import utils, notears_linear

def run_experiment():
    # utils.set_random_seed(1) this line cannot be used to ensure different outcomes in each round of the loop

    n, d, s0, graph_type, sem_type = 1000, 20, 20, 'ER', 'gauss'
    B_true = utils.simulate_dag(d, s0, graph_type)
    W_true = utils.simulate_parameter(B_true)
    np.savetxt('W_true.csv', W_true, delimiter=',')

    X = utils.simulate_linear_sem(W_true, n, sem_type)
    np.savetxt('X.csv', X, delimiter=',')

    W_est = notears_linear(X, lambda1=0.1, loss_type='l2')
    assert utils.is_dag(W_est)
    np.savetxt('W_est.csv', W_est, delimiter=',')
    acc = utils.count_accuracy(B_true, W_est != 0)
    print(acc)

if __name__ == '__main__':
    num_experiments = 3
    total_duration = 0
    
    for idx in range(num_experiments):
        print(f"\n===== 开始第 {idx+1} 次实验 =====")
        start_single = time.time()
        
        run_experiment()
        
        end_single = time.time()
        single_duration = end_single - start_single
        total_duration += single_duration
        
        print(f"第 {idx+1} 次实验耗时: {single_duration:.2f} 秒")
    
    print(f"\n===== 实验全部完成 =====")
    print(f"整个实验循环总运行时间: {total_duration:.2f} 秒")

内容的提问来源于stack exchange,提问作者Jackie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 20:47:26