Skip to content

Commit abd10d6

Browse files
committed
Add benchmarks for calling native class method.
1 parent c9b9a64 commit abd10d6

File tree

3 files changed

+206
-2
lines changed

3 files changed

+206
-2
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
code = """
41+
#include "Python.h"
42+
43+
typedef struct {
44+
PyObject_HEAD;
45+
} NativeCustomObject;
46+
47+
PyObject* nc_classmethod(PyObject* class, PyObject* args) {
48+
return PyTuple_GET_ITEM(args, 0);
49+
}
50+
51+
static struct PyMethodDef nc_methods[] = {
52+
{"customClassMethod", nc_classmethod, METH_VARARGS | METH_CLASS, ""},
53+
{NULL, NULL, 0, NULL}
54+
};
55+
56+
static PyTypeObject NativeCustomType = {
57+
PyVarObject_HEAD_INIT(NULL, 0)
58+
"NativeCustomType.NativeCustomType",
59+
sizeof(NativeCustomObject), /* tp_basicsize */
60+
0, /* tp_itemsize */
61+
0, /* tp_dealloc */
62+
0,
63+
0,
64+
0,
65+
0, /* tp_reserved */
66+
0,
67+
0,
68+
0,
69+
0,
70+
0,
71+
0,
72+
0,
73+
0,
74+
0,
75+
0,
76+
Py_TPFLAGS_DEFAULT,
77+
"",
78+
0, /* tp_traverse */
79+
0, /* tp_clear */
80+
0, /* tp_richcompare */
81+
0, /* tp_weaklistoffset */
82+
0, /* tp_iter */
83+
0, /* tp_iternext */
84+
nc_methods, /* tp_methods */
85+
NULL, /* tp_members */
86+
0, /* tp_getset */
87+
0, /* tp_base */
88+
0, /* tp_dict */
89+
0, /* tp_descr_get */
90+
0, /* tp_descr_set */
91+
0, /* tp_dictoffset */
92+
0, /* tp_init */
93+
PyType_GenericAlloc, /* tp_alloc */
94+
PyType_GenericNew, /* tp_new */
95+
PyObject_Del, /* tp_free */
96+
};
97+
98+
static PyModuleDef c_classmethod_module = {
99+
PyModuleDef_HEAD_INIT,
100+
"c_classmethod_module",
101+
"",
102+
-1,
103+
NULL, NULL, NULL, NULL, NULL
104+
};
105+
106+
PyMODINIT_FUNC
107+
PyInit_c_classmethod(void)
108+
{
109+
PyObject* m;
110+
111+
if (PyType_Ready(&NativeCustomType) < 0)
112+
return NULL;
113+
114+
m = PyModule_Create(&c_classmethod_module);
115+
if (m == NULL)
116+
return NULL;
117+
118+
Py_INCREF(&NativeCustomType);
119+
PyModule_AddObject(m, "NativeCustomType", (PyObject *)&NativeCustomType);
120+
return m;
121+
}
122+
123+
"""
124+
125+
126+
ccompile("c_classmethod", code)
127+
from c_classmethod import NativeCustomType
128+
129+
def count(num):
130+
total = 0
131+
for i in range(num):
132+
total += NativeCustomType.customClassMethod(i)
133+
return total
134+
135+
136+
def measure(num):
137+
result = count(num)
138+
print("result = " + str(result))
139+
140+
141+
def __benchmark__(num=1000000):
142+
measure(num)
143+
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
class CustomType:
41+
@classmethod
42+
def customClassMethod(clazz, *args):
43+
return args[0]
44+
45+
def count(num):
46+
total = 0
47+
for i in range(num):
48+
total += CustomType.customClassMethod(i)
49+
return total
50+
51+
52+
def measure(num):
53+
result = count(num)
54+
print("result = " + str(result))
55+
56+
57+
def __benchmark__(num=1000000):
58+
measure(num)
59+

mx.graalpython/mx_graalpython_bench_param.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@
9696
'c-list-iterating-obj': ITER_15 + ['50000000'],
9797
'magic-bool': ITER_10 + ['100000000'],
9898
'c-magic-bool': ITER_10 + ['100000000'],
99-
'magic-iter': ITER_10 + ['10000000'],
100-
'c-magic-iter': ITER_10 + ['10000000'],
99+
'magic-iter': ITER_10 + ['50000000'],
100+
'c-magic-iter': ITER_10 + ['50000000'],
101101
'instantiation': ITER_10 + ['50000000'],
102102
'c-instantiation': ITER_10 + ['50000000'],
103103
'c_arith-binop': ITER_25 + ['5'],
104104
'c_arith_binop_2': ITER_25 + ['50'],
105+
'call-classmethod': ITER_15 + ['50000000'],
106+
'c-call-classmethod': ITER_15 + ['50000000']
105107
}
106108

107109

0 commit comments

Comments
 (0)