Skip to content

Commit

Permalink
Change to perf_counter_ns
Browse files Browse the repository at this point in the history
  • Loading branch information
Baekalfen committed Oct 22, 2022
1 parent 2a7c308 commit 12416d2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
6 changes: 3 additions & 3 deletions pyboy/plugins/window_sdl2.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
10 changes: 5 additions & 5 deletions pyboy/plugins/window_sdl2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pyboy/pyboy.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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=*)

Expand Down
20 changes: 10 additions & 10 deletions pyboy/pyboy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 12416d2

Please sign in to comment.