【问题标题】:Compute the total time for which the simulation ran in simpy计算模拟在 simpy 中运行的总时间
【发布时间】:2018-10-01 10:49:06
【问题描述】:

我正在使用 env.run() 运行模拟,我的模拟包含一堆进程,它们相互等待并处理一些事件。

现在我知道当我们没有指定模拟应该运行的时间范围时,模拟会一直运行到所有事件都已处理完毕,所以在这种情况下,我如何计算代码中的最终时间(t) , 模拟运行到什么时候?

【问题讨论】:

    标签: python simpy


    【解决方案1】:

    在调用env.run() 后只需使用env.now

    例子:

    import simpy
    
    
    def func1():
        yield env.timeout(50)
        print("Foo bar")
    
    
    def func2():
        yield env.timeout(30)
        print("Hello world")
    
    
    env = simpy.Environment()
    env.process(func1())
    env.process(func2())
    env.run()
    
    print("Simulation ended at: {}".format(env.now))
    

    输出:

    Hello world
    Foo bar
    Simulation ended at: 50
    

    【讨论】:

      【解决方案2】:

      这很简单,您可以检查开始模拟的时间 start_time 和模拟结束的时间 end_time。区别在于模拟运行的时间。

      import time
      start_time = time.time()
      end_time = time.time()
      print end_time  - start_time
      

      【讨论】:

      • 我说的是模拟时间,而不是模拟运行的实时时间,即实时模拟可能已经运行了 2 秒,尽管在模拟环境中,整个过程运行了 150秒。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多