Skip to content

Commit 957e43b

Browse files
committed
Add micro benchmark for magic method '__index__'.
1 parent 0ff8b43 commit 957e43b

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
from . import ccompile
2+
3+
code = """
4+
#include "Python.h"
5+
6+
typedef struct {
7+
PyObject_HEAD;
8+
int bState;
9+
uint64_t cnt;
10+
} NativeMagicMethodsObject;
11+
12+
13+
PyObject* nmm_index(PyObject* self) {
14+
return PyLong_FromLong((((NativeMagicMethodsObject*)self)->cnt)++);
15+
}
16+
17+
int nmm_bool(PyObject* self) {
18+
int curState = ((NativeMagicMethodsObject*)self)->bState;
19+
((NativeMagicMethodsObject*)self)->bState = !curState;
20+
return curState;
21+
}
22+
23+
PyObject* nmm_iter(PyObject* self) {
24+
return NULL;
25+
}
26+
27+
28+
static PyNumberMethods NativeMagicMethods_number_methods = {
29+
0,
30+
0,
31+
0,
32+
0,
33+
0,
34+
0,
35+
0,
36+
0,
37+
0,
38+
nmm_bool,
39+
0,
40+
0,
41+
0,
42+
0,
43+
0,
44+
0,
45+
nmm_index,
46+
0,
47+
0,
48+
0,
49+
0,
50+
0,
51+
0,
52+
0,
53+
0,
54+
0,
55+
0,
56+
0,
57+
0,
58+
0,
59+
0,
60+
0,
61+
0,
62+
nmm_index,
63+
64+
0,
65+
0,
66+
67+
};
68+
69+
static struct PyMethodDef NativeMagicMethods_methods[] = {
70+
{NULL, NULL, 0, NULL},
71+
{NULL, NULL, 0, NULL}
72+
};
73+
74+
static PyTypeObject NativeMagicMethodsType = {
75+
PyVarObject_HEAD_INIT(NULL, 0)
76+
"NativeMagicMethods.NativeMagicMethods",
77+
sizeof(NativeMagicMethodsObject), /* tp_basicsize */
78+
0, /* tp_itemsize */
79+
0, /* tp_dealloc */
80+
0,
81+
0,
82+
0,
83+
0, /* tp_reserved */
84+
0,
85+
&NativeMagicMethods_number_methods,
86+
0,
87+
0,
88+
0,
89+
0,
90+
0,
91+
0,
92+
0,
93+
0,
94+
Py_TPFLAGS_DEFAULT,
95+
"",
96+
0, /* tp_traverse */
97+
0, /* tp_clear */
98+
0, /* tp_richcompare */
99+
0, /* tp_weaklistoffset */
100+
nmm_iter, /* tp_iter */
101+
0, /* tp_iternext */
102+
NativeMagicMethods_methods, /* tp_methods */
103+
NULL, /* tp_members */
104+
0, /* tp_getset */
105+
0, /* tp_base */
106+
0, /* tp_dict */
107+
0, /* tp_descr_get */
108+
0, /* tp_descr_set */
109+
0, /* tp_dictoffset */
110+
0, /* tp_init */
111+
PyType_GenericAlloc, /* tp_alloc */
112+
PyType_GenericNew, /* tp_new */
113+
PyObject_Del, /* tp_free */
114+
};
115+
116+
static PyModuleDef NativeMagicMethodsmodule = {
117+
PyModuleDef_HEAD_INIT,
118+
"c_list_iterating_obj_module",
119+
"",
120+
-1,
121+
NULL, NULL, NULL, NULL, NULL
122+
};
123+
124+
PyMODINIT_FUNC
125+
PyInit_c_list_iterating_obj_module(void)
126+
{
127+
PyObject* m;
128+
129+
130+
if (PyType_Ready(&NativeMagicMethodsType) < 0)
131+
return NULL;
132+
133+
134+
m = PyModule_Create(&NativeMagicMethodsmodule);
135+
if (m == NULL)
136+
return NULL;
137+
138+
Py_INCREF(&NativeMagicMethodsType);
139+
PyModule_AddObject(m, "NativeMagicMethods", (PyObject *)&NativeMagicMethodsType);
140+
return m;
141+
}
142+
143+
"""
144+
145+
146+
ccompile("c_list_iterating_obj_module", code)
147+
from . import c_list_iterating_obj_module
148+
149+
def iterate_list(ll, num):
150+
idxObj = c_list_iterating_obj_module.NativeMagicMethods()
151+
for t in range(num):
152+
item = ll[idxObj]
153+
return item
154+
155+
156+
def measure(num):
157+
last_item = iterate_list(list(range(num)), num)
158+
print("Last item ", last_item)
159+
160+
161+
def __benchmark__(num=1000000):
162+
measure(num)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Idx:
2+
__cnt = 0
3+
def __index__(self):
4+
cur = self.__cnt
5+
self.__cnt += 1
6+
return cur
7+
8+
def iterate_list(ll, num):
9+
idxObj = Idx()
10+
for t in range(num):
11+
item = ll[idxObj]
12+
return item
13+
14+
15+
def measure(num):
16+
last_item = iterate_list(list(range(num)), num)
17+
print("Last item ", last_item)
18+
19+
20+
def __benchmark__(num=1000000):
21+
measure(num)

mx.graalpython/mx_graalpython_bench_param.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
'list-indexing': ITER_15 + ['1000000'],
8383
'list-iterating-explicit': ITER_25 + ['1000000'],
8484
'list-iterating': ITER_25 + ['1000000'],
85+
'list-iterating-obj': ITER_15 + ['10000000'],
8586
'math-sqrt': ITER_15 + ['500000000'],
8687
'object-allocate': ITER_10 + ['5000'],
8788
'object-layout-change': ITER_15 + ['1000000'],

0 commit comments

Comments
 (0)