0

I want to be able to calculate the number of counts after a given time interval

d ={}
now = datetime.now()

d[now] = count

'count' is from another variable but it continuously updates the dictionary as below. The dictionary d[now] looks like this:

2020-03-27 13:36:21.060325 -> 0
2020-03-27 13:36:37.067294 -> 5
2020-03-27 13:36:40.836107 -> 6
2020-03-27 13:36:41.097320 -> 6
2020-03-27 13:36:42.864630 -> 5
2020-03-27 13:36:42.985437 -> 5

difference between start time and finish time rate = sum of counts/time taken

loyclay
  • 1
  • 3
  • Can you give an example of the input and expected output of the method you need? Will the given time interval be one of the keys? – Iain Shelvington Mar 27 '20 at 04:53
  • At the start time, the count is 0 and after 5 seconds the count is say 10. Finally i want to obtain the count rate = (sum of counts)//time taken. My problem is being able to subtract the start and finish time from the datetime key values. The time interval should just be an integer or float value i can divide from the sum of the value components. – loyclay Mar 27 '20 at 05:05
  • How do i subtract the keys, in these case only interested in seconds and microsecs. – loyclay Mar 27 '20 at 05:10
  • As you are adding a datetime object as keys you can use it to execute datetime operations in the keys. Have a look here where someone shows how to subtract two datetime objects from each other. https://stackoverflow.com/a/40492588/8608854 the Python ```timedelta``` has a resolution of microseconds. – Johnny_xy Mar 27 '20 at 05:16
  • I had tried:>>>> for i in range (len(d)): sum_count = sum(d.values()) print(sum_count) diff_time = now - d[i] print(sum_count/diff_time) – loyclay Mar 27 '20 at 05:20
  • I am not sure this will work as you divide a integer value with a datetime-object. Modify it with this: ```print(sum_count/diff_time.microseconds)``` Still, it would be really helpful if you provide a minimal example we can talk about :). – Johnny_xy Mar 27 '20 at 05:24

1 Answers1

0

Use time instead.

from time import time

def getCountRate(max_count):
    count = 0
    start_of_count = time()
    for i in range(max_count):
        count+=1
        # some other code if you want
    end_of_count = time()
    count_rate = count/(end_of_count-start_of_count)
    return count_rate
AGawish
  • 98
  • 1
  • 6