Skip to content

Commit

Permalink
new clock() using internal Python API
Browse files Browse the repository at this point in the history
  • Loading branch information
slayoo committed Jan 2, 2024
1 parent a9b64c4 commit 0a6d39a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 23 deletions.
18 changes: 4 additions & 14 deletions PyMPDATA/impl/clock.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
""" CPU-time returning clock() function which works from within njit-ted code """
""" CPU-time returning clock() function which works from within njit-ted code,
no time unit guaranteed, returned value only for relative time measurements """
import ctypes
import platform

if platform.system() == "Windows":
from ctypes.util import find_msvcrt

__LIB = find_msvcrt()
if __LIB is None:
__LIB = "msvcrt.dll"
else:
from ctypes.util import find_library

__LIB = find_library("c")

clock = ctypes.CDLL(__LIB).clock
clock = ctypes.pythonapi._PyTime_GetSystemClock
clock.argtypes = []
clock.restype = ctypes.c_int64
4 changes: 0 additions & 4 deletions tests/smoke_tests/olesik_et_al_2022/test_wall_time.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# pylint: disable=missing-module-docstring,missing-class-docstring,missing-function-docstring
import os

import pytest
from PyMPDATA_examples.Olesik_et_al_2022 import wall_time


@pytest.mark.xfail(condition=os.name == "nt")
def test_wall_time():
wall_time.test_wall_time()
13 changes: 8 additions & 5 deletions tests/unit_tests/test_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ def test():
@staticmethod
def test_clock_value():
# Arrange
sec_expected = 2
factor = 3
base = 0.01
start = clock()
time.sleep(base)
sec_one = clock() - start

# Act
time.sleep(sec_expected)
stop = clock()
start = clock()
time.sleep(base * factor)
sec_two = clock() - start

# Assert
sec_actual = (stop - start) / 1000
assert (sec_actual - sec_expected) / sec_expected < 0.1
assert abs(sec_two / sec_one - factor) < 0.1

0 comments on commit 0a6d39a

Please sign in to comment.