From 12416d227639470a29471b31347119e10bf626fb Mon Sep 17 00:00:00 2001 From: Mads Ynddal <5528170+Baekalfen@users.noreply.github.com> Date: Sat, 22 Oct 2022 20:24:42 +0200 Subject: [PATCH] Change to perf_counter_ns --- pyboy/plugins/window_sdl2.pxd | 6 +++--- pyboy/plugins/window_sdl2.py | 10 +++++----- pyboy/pyboy.pxd | 3 ++- pyboy/pyboy.py | 20 ++++++++++---------- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/pyboy/plugins/window_sdl2.pxd b/pyboy/plugins/window_sdl2.pxd index 5d0bc37f7..3f900b799 100644 --- a/pyboy/plugins/window_sdl2.pxd +++ b/pyboy/plugins/window_sdl2.pxd @@ -8,7 +8,7 @@ cimport pyboy.utils import cython cimport cython -from libc.stdint cimport uint8_t, uint16_t, int16_t, uint32_t +from libc.stdint cimport int64_t, uint8_t, uint16_t, int16_t, uint32_t cdef int ROWS, COLS @@ -20,7 +20,7 @@ cpdef list sdl2_event_pump(list) cdef class WindowSDL2(PyBoyWindowPlugin): - cdef double _ftime + cdef int64_t _ftime cdef dict _key_down cdef dict _key_up cdef bint fullscreen @@ -29,5 +29,5 @@ cdef class WindowSDL2(PyBoyWindowPlugin): cdef object _sdlrenderer cdef object _sdltexturebuffer - @cython.locals(now=double, delay=cython.int) + @cython.locals(now=int64_t, delay=int64_t) cdef bint frame_limiter(self, int) diff --git a/pyboy/plugins/window_sdl2.py b/pyboy/plugins/window_sdl2.py index a0c80195d..45eaef0a1 100644 --- a/pyboy/plugins/window_sdl2.py +++ b/pyboy/plugins/window_sdl2.py @@ -4,7 +4,7 @@ # import logging -from time import perf_counter +import time from pyboy.plugins.base_plugin import PyBoyWindowPlugin from pyboy.utils import WindowEvent, WindowEventMouse @@ -157,7 +157,7 @@ def __init__(self, pyboy, mb, pyboy_argv): return sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO | sdl2.SDL_INIT_GAMECONTROLLER) - self._ftime = 0.0 + self._ftime = time.perf_counter_ns() self._window = sdl2.SDL_CreateWindow( b"PyBoy", sdl2.SDL_WINDOWPOS_CENTERED, sdl2.SDL_WINDOWPOS_CENTERED, self._scaledresolution[0], @@ -204,10 +204,10 @@ def enabled(self): return False def frame_limiter(self, speed): - self._ftime += 1.0 / (60.0*speed) - now = perf_counter() + self._ftime += int((1.0 / (60.0*speed)) * 1_000_000_000) + now = time.perf_counter_ns() if (self._ftime > now): - delay = int(1000 * (self._ftime - now)) + delay = (self._ftime - now) // 1_000_000 sdl2.SDL_Delay(delay) else: self._ftime = now diff --git a/pyboy/pyboy.pxd b/pyboy/pyboy.pxd index 1eef1ded6..b3d5eb45e 100644 --- a/pyboy/pyboy.pxd +++ b/pyboy/pyboy.pxd @@ -10,6 +10,7 @@ from libc.stdint cimport uint64_t from pyboy.core.mb cimport Motherboard from pyboy.utils cimport IntIOWrapper, IntIOInterface from pyboy.plugins.manager cimport PluginManager +from libc.stdint cimport int64_t cdef double SPF @@ -41,7 +42,7 @@ cdef class PyBoy: cdef list recorded_input cdef list external_input - @cython.locals(t_start=double, t_pre=double, t_tick=double, t_post=double, secs=double) + @cython.locals(t_start=int64_t, t_pre=int64_t, t_tick=int64_t, t_post=int64_t, nsecs=int64_t) cpdef bint tick(self) cpdef void stop(self, save=*) diff --git a/pyboy/pyboy.py b/pyboy/pyboy.py index a947e68b8..63139c742 100644 --- a/pyboy/pyboy.py +++ b/pyboy/pyboy.py @@ -125,27 +125,27 @@ def tick(self): if self.stopped: return True - t_start = time.perf_counter() # Change to _ns when PyPy supports it + t_start = time.perf_counter_ns() self._handle_events(self.events) - t_pre = time.perf_counter() + t_pre = time.perf_counter_ns() if not self.paused: if self.mb.tick(): # breakpoint reached self.plugin_manager.handle_breakpoint() else: self.frame_count += 1 - t_tick = time.perf_counter() + t_tick = time.perf_counter_ns() self._post_tick() - t_post = time.perf_counter() + t_post = time.perf_counter_ns() - secs = t_pre - t_start - self.avg_pre = 0.9 * self.avg_pre + 0.1*secs + nsecs = t_pre - t_start + self.avg_pre = 0.9 * self.avg_pre + (0.1*nsecs/1_000_000_000) - secs = t_tick - t_pre - self.avg_tick = 0.9 * self.avg_tick + 0.1*secs + nsecs = t_tick - t_pre + self.avg_tick = 0.9 * self.avg_tick + (0.1*nsecs/1_000_000_000) - secs = t_post - t_tick - self.avg_post = 0.9 * self.avg_post + 0.1*secs + nsecs = t_post - t_tick + self.avg_post = 0.9 * self.avg_post + (0.1*nsecs/1_000_000_000) return self.quitting