【问题标题】:Count time between messages CANBUS计算消息之间的时间 CANBUS
【发布时间】:2015-03-05 07:31:00
【问题描述】:

我无法为我的脚本设计一个可行的计时器。

timeit.timeit() 会大大降低性能,time.clock 会不断增加(因为收集的时钟时间会不断增加),并且使用数据流中的内置计时方法会提供不一致的结果。

请参阅下面的代码以及我的 2 次尝试。

#!/usr/bin/python

import subprocess,time, timeit
import re, os, pprint, math
from collections import defaultdict
from Tkinter import *

DICT = {}
COUNT = defaultdict(int)
CURRSTAMP = 0

def RECEIVE(COMMAND):
    PROCESS = subprocess.Popen(COMMAND, stdout=subprocess.PIPE)
    LINES = iter(PROCESS.stdout.readline, "")
    try:
        START = time.clock() - constantly increases and does not set back to 0 after iteration
        #TIMER = "0"
        for LINE in LINES:
            if re.match(r"^\d+.*$",LINE):
                SPLITLINE = LINE.split()
#               CURRSTAMP = float(TIMER)
                del SPLITLINE[1:4]
                TIMER = SPLITLINE[0]
    #           TIMING =(float(TIMER) - CURRSTAMP) * 1000
                IDENTIFIER = SPLITLINE[1]
                COUNT[IDENTIFIER] += 1
                END = time.clock()
                TIMING = (END - START) *1000
                DLC = SPLITLINE[2]
                HEXBITS = SPLITLINE[3:]
                DICT[IDENTIFIER] = [DLC, HEXBITS, COUNT[IDENTIFIER],TIMING]
                time.sleep(0.005)
                for IDENTIFIER, HEXBITS in DICT.items():
                    os.system("clear")
                    pprint.pprint(DICT, width = 150)
        START = 0
    except KeyboardInterrupt:
        pass

time.clock() 似乎具有我需要的执行速度,但我必须遗漏一些东西,因为它在每个迭代器上递增 0.10。

数据:

receivetest Version "Release_20090203_n"  (www.peak-system.com)
------- Copyright (C) 2004-2009 PEAK System-Technik GmbH ------
receivetest comes with ABSOLUTELY NO WARRANTY.     This is free
software  and you are welcome  to redistribute it under certain
conditions.   For   details   see    attached   COPYING   file.

receivetest: device node="/dev/pcan32"
             Only standard frames are accepted, init with 500 kbit/sec.
receivetest: driver version = Release_20141017_n
8827710.77 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8827710.546 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8827711.16 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8827711.485 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8827810.45 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8827810.514 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8827810.984 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8827811.453 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8827910.56 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8827910.525 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8827910.994 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8827911.464 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828010.66 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828010.536 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828011.5 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828011.474 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828110.77 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828110.546 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828111.16 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828111.485 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828210.45 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828210.514 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828210.984 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828211.453 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828310.184 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828310.610 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828311.80 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828311.549 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828410.66 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828410.536 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828411.5 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828411.474 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828510.77 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828510.546 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828511.16 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828511.485 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828610.88 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828610.557 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828610.984 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828611.496 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828710.98 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828710.568 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828711.37 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828711.506 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828810.109 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828810.578 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828811.48 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828811.517 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8828910.120 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8828910.546 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8828911.16 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8828911.485 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8829010.88 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829010.557 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8829011.26 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829011.496 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8829110.98 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829110.568 receivetest: m s 0x00000690 8 00 18 00 00 00 83 59 1a 
8829111.37 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8829111.506 receivetest: m s 0x00000691 8 01 38 9f 65 50 5a 06 00 
8829111.976 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829210.109 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829210.578 receivetest: m s 0x00000691 8 02 3f 03 67 0c 03 0a 00 
8829211.48 receivetest: m s 0x00000690 8 00 18 00 00 00 83 59 1a 
8829211.517 receivetest: m s 0x00000663 8 2f 00 62 02 00 03 0a 00 
8829211.986 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829310.120 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829310.589 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8829311.58 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829311.528 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8829410.88 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829410.557 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829411.26 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8829411.496 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8829425.746 receivetest: m s 0x00000688 8 02 00 00 5b 86 6a d2 ac 
8829510.141 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829510.610 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8829511.37 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829511.549 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 
8829610.109 receivetest: m s 0x0000069a 8 00 00 00 00 00 83 59 1a 
8829610.578 receivetest: m s 0x0000069e 8 00 1f 5e 28 35 83 59 1a 
8829611.48 receivetest: m s 0x00000663 8 2f 00 62 02 00 83 59 1a 
8829611.517 receivetest: m s 0x00000690 8 00 18 00 28 35 83 59 1a 

任何帮助将不胜感激。

【问题讨论】:

  • 当你说计时器 - 什么是重要的?这是用于用户通知、基准测试、记录保存吗?一些选项将是可行的,而另一些则不取决于您想要什么。 timeit 专为基准测试而设计,将多次运行代码以尝试获得准确的时间结果(因此“慢”)。如果您想要用户的挂钟时差,只需使用与上一次的差来计算每次迭代的时间。
  • 另外,请注意,全大写的变量名是一种非常难以阅读的风格,与the standard most follow 背道而驰。
  • 监控消息之间的时间间隔,这样我就可以知道消息发送的频率/频率
  • timeit 绝对不是您想要的工具。它需要有多准确(例如,如果用户更改时钟时间或有闰秒,是否说它花费了负时间是否重要?)。如果只是为了能够大致了解您的消息是如何通过的,那么使用time.clock() 就可以了。只需使用time.clock() - START 即可获得与上次的差异,这正是您想要的。 (并且不要重置START)。
  • 谢谢@Lattyware,但我已经尝试过time.clock(),但是当它不断打印值并不断增加时,我刚刚尝试使用您的time.clock() - START方法,它仍然增加了1毫秒每次迭代,增加的时间看起来都是正确的,只是我不完全添加它们

标签: python linux time timer data-stream


【解决方案1】:

问题是您要获取开始时间,然后计算出每次迭代的开始时间与结束时间之间的差异。您需要将开始时间设置为上一次迭代的结束时间,以获取从那以后的时间。

start = time.clock()
for line in lines:
   ...
   end = time.clock()
   timeTaken = end - start
   start = end

例子:

time:  1     2     3     4     5
event: start       i1          i2

你在做什么:

i1:    ------------> (2)
i2:    ------------------------> (4)

你想要什么:

i1:    ------------> (2)
i2:                 -----------> (2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 2016-09-18
    相关资源
    最近更新 更多