cython

Форк
0
/
time_pxd.pyx 
157 строк · 4.3 Кб
1
# mode: run
2
# tag: py3only,pytime
3

4
import time
5

6
from cpython cimport time as ctime
7

8
import sys
9
cdef bint IS_PYPY = hasattr(sys, 'pypy_version_info')
10

11

12
def test_time():
13
    """
14
    >>> tic1, tic2, tic3 = test_time()
15
    >>> tic1 <= tic3  or  (tic1, tic3)  # sanity check
16
    True
17
    >>> tic1 <= tic2  or  (tic1, tic2)
18
    True
19
    >>> tic2 <= tic3  or  (tic2, tic3)
20
    True
21
    """
22
    # check that C-API matches Py-API to within call-time tolerance
23
    tic1 = time.time()
24
    tic2 = ctime.time()
25
    tic3 = time.time()
26

27
    return tic1, tic2, tic3
28

29

30
def test_time_ns():
31
    """
32
    >>> tic1, tic2, tic3 = test_time_ns()
33
    >>> tic1 <= tic3  or  (tic1, tic3)  # sanity check
34
    True
35
    >>> tic1 <= tic2  or  (tic1, tic2)
36
    True
37
    >>> tic2 <= tic3  or  (tic2, tic3)
38
    True
39
    """
40
    # check that C-API matches Py-API to within call-time tolerance
41
    tic1 = time.time_ns()
42
    tic2 = ctime.time_ns()
43
    tic3 = time.time_ns()
44

45
    return tic1, tic2, tic3
46

47

48
def test_perf_counter():
49
    """
50
    >>> tic1, tic2, tic3 = test_perf_counter()
51
    >>> tic1 <= tic3  or  (tic1, tic3)  # sanity check
52
    True
53
    >>> tic1 <= tic2  or  (tic1, tic2)
54
    True
55
    >>> tic2 <= tic3  or  (tic2, tic3)
56
    True
57
    """
58
    # check that C-API matches Py-API to within call-time tolerance
59
    tic1 = time.perf_counter()  if not IS_PYPY else  ctime.perf_counter()
60
    tic2 = ctime.perf_counter()
61
    tic3 = time.perf_counter()  if not IS_PYPY else  ctime.perf_counter()
62

63
    return tic1, tic2, tic3
64

65

66
def test_perf_counter_ns():
67
    """
68
    >>> tic1, tic2, tic3 = test_perf_counter_ns()
69
    >>> tic1 <= tic3  or  (tic1, tic3)  # sanity check
70
    True
71
    >>> tic1 <= tic2  or  (tic1, tic2)
72
    True
73
    >>> tic2 <= tic3  or  (tic2, tic3)
74
    True
75
    """
76
    # check that C-API matches Py-API to within call-time tolerance
77
    tic1 = time.perf_counter_ns()  if not IS_PYPY else  ctime.perf_counter_ns()
78
    tic2 = ctime.perf_counter_ns()
79
    tic3 = time.perf_counter_ns()  if not IS_PYPY else  ctime.perf_counter_ns()
80

81
    return tic1, tic2, tic3
82

83

84
def test_monotonic():
85
    """
86
    >>> tic1, tic2, tic3 = test_monotonic()
87
    >>> tic1 <= tic3  or  (tic1, tic3)  # sanity check
88
    True
89
    >>> tic1 <= tic2  or  (tic1, tic2)
90
    True
91
    >>> tic2 <= tic3  or  (tic2, tic3)
92
    True
93
    """
94
    # check that C-API matches Py-API to within call-time tolerance
95
    tic1 = time.monotonic()  if not IS_PYPY else  ctime.monotonic()
96
    tic2 = ctime.monotonic()
97
    tic3 = time.monotonic()  if not IS_PYPY else  ctime.monotonic()
98

99
    return tic1, tic2, tic3
100

101

102
def test_monotonic_ns():
103
    """
104
    >>> tic1, tic2, tic3 = test_monotonic_ns()
105
    >>> tic1 <= tic3  or  (tic1, tic3)  # sanity check
106
    True
107
    >>> tic1 <= tic2  or  (tic1, tic2)
108
    True
109
    >>> tic2 <= tic3  or  (tic2, tic3)
110
    True
111
    """
112
    # check that C-API matches Py-API to within call-time tolerance
113
    tic1 = time.monotonic_ns()  if not IS_PYPY else  ctime.monotonic_ns()
114
    tic2 = ctime.monotonic_ns()
115
    tic3 = time.monotonic_ns()  if not IS_PYPY else  ctime.monotonic_ns()
116

117
    return tic1, tic2, tic3
118

119

120
def test_localtime():
121
    """
122
    >>> ltp, ltc = test_localtime()
123
    >>> ltp.tm_year == ltc['tm_year']  or  (ltp.tm_year, ltc['tm_year'])
124
    True
125
    >>> ltp.tm_mon == ltc['tm_mon']  or  (ltp.tm_mon, ltc['tm_mon'])
126
    True
127
    >>> ltp.tm_mday == ltc['tm_mday']  or  (ltp.tm_mday, ltc['tm_mday'])
128
    True
129
    >>> ltp.tm_hour == ltc['tm_hour']  or  (ltp.tm_hour, ltc['tm_hour'])
130
    True
131
    >>> ltp.tm_min == ltc['tm_min']  or  (ltp.tm_min, ltc['tm_min'])
132
    True
133
    >>> ltp.tm_sec == ltc['tm_sec']  or  (ltp.tm_sec, ltc['tm_sec'])
134
    True
135
    >>> ltp.tm_wday == ltc['tm_wday']  or (ltp.tm_wday, ltc['tm_wday'])
136
    True
137
    >>> ltp.tm_yday == ltc['tm_yday']  or  (ltp.tm_yday, ltc['tm_yday'])
138
    True
139
    >>> ltp.tm_isdst == ltc['tm_isdst']  or  (ltp.tm_isdst, ltc['tm_isdst'])
140
    True
141
    """
142
    ltp = time.localtime()
143
    ltc = ctime.localtime()
144

145
    i = 0
146
    while ltp.tm_sec != ltc.tm_sec:
147
        # If the time.localtime call is just before the end of a second and the
148
        #  ctime.localtime call is just after the beginning of the next second,
149
        #  re-call.  This should not occur twice in a row.
150
        time.sleep(0.1)
151
        ltp = time.localtime()
152
        ltc = ctime.localtime()
153
        i += 1
154
        if i > 10:
155
            break
156

157
    return ltp, ltc
158

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.