Skip to content

Instantly share code, notes, and snippets.

@PeterMinin
Created May 15, 2016 18:15
Show Gist options
  • Save PeterMinin/3f4a9f9302d8c864c5b9046771c3b2f7 to your computer and use it in GitHub Desktop.
Save PeterMinin/3f4a9f9302d8c864c5b9046771c3b2f7 to your computer and use it in GitHub Desktop.
Python utils
from time import time
class FpsCounter(object):
def __init__(self, period=1, period_in_seconds=True):
self.frame_counter = None
self.interval_start = None
self.period = period
self.period_in_seconds = period_in_seconds
self.total_seconds = 0
self.average = 0
def tick(self):
cur_time = time()
if self.frame_counter is not None:
self.frame_counter += 1
if self.period_in_seconds:
interval = cur_time - self.interval_start
if interval >= self.period:
self._update(cur_time, interval)
elif self.frame_counter == self.period:
self._update(cur_time, cur_time - self.interval_start)
else:
self._reset(cur_time)
def notify_pause(self, pause_length):
self.interval_start += pause_length
def _reset(self, cur_time):
self.frame_counter = 0
self.interval_start = cur_time
def _update(self, cur_time, interval):
fps = self.frame_counter / interval
new_total_seconds = self.total_seconds + interval
self.average = (self.average * self.total_seconds + fps * interval) / new_total_seconds
self.total_seconds = new_total_seconds
print("FPS: {:.1f}, average: {:.1f}".format(fps, self.average))
self._reset(cur_time)
import os
def path_full_split(path):
parts = []
while True:
new_path, part = os.path.split(path)
if part:
parts.append(part)
if not new_path or new_path == path:
if new_path:
parts.append(path)
break
path = new_path
parts.reverse()
return parts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment