Skip to content

Commit 673f85d

Browse files
committed
simple arith-binop of benchmark for float subclass
1 parent 22bd02a commit 673f85d

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Copyright (c) 2017, 2018, Oracle and/or its affiliates.
2+
# Copyright (c) 2013, Regents of the University of California
3+
#
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are
7+
# permitted provided that the following conditions are met:
8+
#
9+
# 1. Redistributions of source code must retain the above copyright notice, this list of
10+
# conditions and the following disclaimer.
11+
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
16+
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17+
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
18+
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19+
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20+
# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21+
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22+
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23+
# OF THE POSSIBILITY OF SUCH DAMAGE.
24+
# arithmetic ops (partially extracted from spectralnorm)
25+
26+
code = """
27+
#include <Python.h>
28+
#include "structmember.h"
29+
30+
static PyTypeObject FloatSubclass = {
31+
PyVarObject_HEAD_INIT(NULL, 0)
32+
"FloatSubclass", /* tp_name */
33+
sizeof(PyFloatObject), /* tp_basicsize */
34+
0, /* tp_itemsize */
35+
0, /* tp_dealloc */
36+
0, /* tp_print */
37+
0, /* tp_getattr */
38+
0, /* tp_setattr */
39+
0, /* tp_reserved */
40+
0, /* tp_repr */
41+
0, /* tp_as_number */
42+
0, /* tp_as_sequence */
43+
0, /* tp_as_mapping */
44+
0, /* tp_hash */
45+
0, /* tp_call */
46+
0, /* tp_str */
47+
0, /* tp_getattro */
48+
0, /* tp_setattro */
49+
0, /* tp_as_buffer */
50+
Py_TPFLAGS_DEFAULT |
51+
Py_TPFLAGS_BASETYPE, /* tp_flags */
52+
"", /* tp_doc */
53+
0, /* tp_traverse */
54+
0, /* tp_clear */
55+
0, /* tp_richcompare */
56+
0, /* tp_weaklistoffset */
57+
0, /* tp_iter */
58+
0, /* tp_iternext */
59+
0, /* tp_methods */
60+
0, /* tp_members */
61+
0, /* tp_getset */
62+
&PyFloat_Type, /* tp_base */
63+
0, /* tp_dict */
64+
0, /* tp_descr_get */
65+
0, /* tp_descr_set */
66+
0, /* tp_dictoffset */
67+
0, /* tp_init */
68+
0, /* tp_alloc */
69+
0, /* tp_new */
70+
};
71+
72+
static PyModuleDef module = {
73+
PyModuleDef_HEAD_INIT,
74+
"module",
75+
"",
76+
-1,
77+
NULL, NULL, NULL, NULL, NULL
78+
};
79+
80+
PyMODINIT_FUNC
81+
PyInit_c_arith_binop_module(void) {
82+
PyType_Ready(&FloatSubclass);
83+
PyObject* m = PyModule_Create(&module);
84+
Py_INCREF(&FloatSubclass);
85+
PyModule_AddObject(m, "FloatSubclass", (PyObject *)&FloatSubclass);
86+
return m;
87+
}
88+
"""
89+
90+
91+
ccompile("c_arith_binop_module", code)
92+
from c_arith_binop_module import FloatSubclass
93+
94+
95+
def docompute(num):
96+
for i in range(num):
97+
sum_ = FloatSubclass(0.0)
98+
j = FloatSubclass(0)
99+
while j < num:
100+
sum_ += 1.0 / ((i + j) * (i + j + 1) + i + 1)
101+
j += 1
102+
103+
return sum_
104+
105+
106+
def measure(num):
107+
for run in range(num):
108+
sum_ = docompute(1000)
109+
print("sum", sum_)
110+
111+
112+
def __benchmark__(num=5):
113+
measure(num)

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/floats/FloatBuiltins.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,28 @@ abstract static class AddNode extends PythonBinaryBuiltinNode {
206206
return left + right.doubleValue();
207207
}
208208

209+
@Specialization
210+
Object doDP(PythonNativeObject left, long right,
211+
@Cached("nativeFloat()") FromNativeSubclassNode<Double> getFloat) {
212+
Double leftPrimitive = getFloat.execute(left);
213+
if (leftPrimitive != null) {
214+
return leftPrimitive + right;
215+
} else {
216+
return PNotImplemented.NOT_IMPLEMENTED;
217+
}
218+
}
219+
220+
@Specialization
221+
Object doDP(PythonNativeObject left, double right,
222+
@Cached("nativeFloat()") FromNativeSubclassNode<Double> getFloat) {
223+
Double leftPrimitive = getFloat.execute(left);
224+
if (leftPrimitive != null) {
225+
return leftPrimitive + right;
226+
} else {
227+
return PNotImplemented.NOT_IMPLEMENTED;
228+
}
229+
}
230+
209231
@SuppressWarnings("unused")
210232
@Fallback
211233
PNotImplemented doGeneric(Object left, Object right) {

mx.graalpython/mx_graalpython_bench_param.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@
9797
'magic-iter': ITER_10 + ['10000000'],
9898
'c-magic-iter': ITER_10 + ['10000000'],
9999
'instantiation': ITER_10 + ['50000000'],
100-
'c-instantiation': ITER_10 + ['50000000']
101-
100+
'c-instantiation': ITER_10 + ['50000000'],
101+
'c-magic-bool': ITER_10 + ['100000000'],
102+
'c_arith-binop': ITER_25 + ['5'],
102103
}
103104

104105

0 commit comments

Comments
 (0)