0

I'm having trouble coming up with a viable time counter for my script.

timeit.timeit() slows performance too much, time.clock constantly increase ( as gather as this is running off an ever increase clock time) and using the in-built timing method from the data stream is providing inconsistant results.

Please see code below with 2 of my attempts.

#!/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() seems to have the speed of execution I need but I must be missing something as it increments by .10 on each iterator.

DATA:

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 

Any help would be much appreciated.

Jalcock501
  • 389
  • 2
  • 6
  • 18
  • When you say timer - what is important? Is this for user notification, benchmarking, record keeping? Some options will be viable and others not depending on what you want. `timeit` is designed for benchmarking, and will run the code many times to try and get an accurate time result (hence the 'slow'). If you want wall-clock time difference for the user, just use the difference from the previous time to work out the time per iteration. – Gareth Latty Jan 07 '15 at 11:58
  • Also, as a note, all-caps variable names is a really hard-to-read style that goes against [the standard most follow](https://www.python.org/dev/peps/pep-0008/). – Gareth Latty Jan 07 '15 at 12:00
  • It's to monitor the time intervals between messages so I can tell how frequent/infrequent the messages are coming through – Jalcock501 Jan 07 '15 at 12:00
  • `timeit` definitely isn't the tool you want then. How accurate does it need to be (if the user changes the clock time or there is a leap second, does it matter if it says it took negative time, for example?). If it's literally just to be able to see roughly how your messages are coming through, then using `time.clock()` is fine. Just use `time.clock() - START` to get the difference from the last time, which is what you seem to want. (And don't reset `START`). – Gareth Latty Jan 07 '15 at 12:02
  • Thank you @Lattyware but I have tried `time.clock()` but when it prints the value contantly and consistently increases, I have just attempted it with your method of `time.clock() - START` and it is still increasing by 1 millisecond every iteration, the increase look to be the correct amount of time, it's just I don't what to add them altogether – Jalcock501 Jan 07 '15 at 12:06
  • Apologies, I didn't read your code quite carefully enough, and misunderstood what you were currently doing. See my answer. – Gareth Latty Jan 07 '15 at 12:13

1 Answers1

1

The issue is that you are getting the start time, then working out the difference between that and the end time of each iteration. You need to set the start time to the end time of the previous iteration to get the time since that.

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

Example:

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

What you are doing:

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

What you want:

i1:    ------------> (2)
i2:                 -----------> (2)
Gareth Latty
  • 86,389
  • 17
  • 178
  • 183