Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

meters.py 1.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  1. # Copyright (c) 2017-present, Facebook, Inc.
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the license found in the LICENSE file in
  5. # the root directory of this source tree. An additional grant of patent rights
  6. # can be found in the PATENTS file in the same directory.
  7. import time
  8. class AverageMeter(object):
  9. """Computes and stores the average and current value"""
  10. def __init__(self):
  11. self.reset()
  12. def reset(self):
  13. self.val = 0
  14. self.avg = 0
  15. self.sum = 0
  16. self.count = 0
  17. def update(self, val, n=1):
  18. self.val = val
  19. self.sum += val * n
  20. self.count += n
  21. self.avg = self.sum / self.count
  22. class TimeMeter(object):
  23. """Computes the average occurrence of some event per second"""
  24. def __init__(self, init=0):
  25. self.reset(init)
  26. def reset(self, init=0):
  27. self.init = init
  28. self.start = time.time()
  29. self.n = 0
  30. def update(self, val=1):
  31. self.n += val
  32. @property
  33. def avg(self):
  34. return self.n / self.elapsed_time
  35. @property
  36. def elapsed_time(self):
  37. return self.init + (time.time() - self.start)
  38. class StopwatchMeter(object):
  39. """Computes the sum/avg duration of some event in seconds"""
  40. def __init__(self):
  41. self.reset()
  42. def start(self):
  43. self.start_time = time.time()
  44. def stop(self, n=1):
  45. if self.start_time is not None:
  46. delta = time.time() - self.start_time
  47. self.sum += delta
  48. self.n += n
  49. self.start_time = None
  50. def reset(self):
  51. self.sum = 0
  52. self.n = 0
  53. self.start_time = None
  54. @property
  55. def avg(self):
  56. return self.sum / self.n
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...