|
| 1 | +# Copyright (c) 2019, 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 | +static PyObject* nc_method_varargs(PyObject* self, PyObject* args, PyObject* kwds) { |
| 48 | + char *kwdnames[] = { "a", "b", "c", NULL}; |
| 49 | + PyObject* longobj = NULL; |
| 50 | + PyObject* result = NULL; |
| 51 | + int ival = 0; |
| 52 | + long lval = 0; |
| 53 | + long lval2 = 0; |
| 54 | +
|
| 55 | + Py_XINCREF(args); |
| 56 | + Py_XINCREF(kwds); |
| 57 | + if (!PyArg_ParseTupleAndKeywords(args, kwds, "Oil:uff", kwdnames, &longobj, &ival, &lval)) { |
| 58 | + return NULL; |
| 59 | + } |
| 60 | + Py_XDECREF(args); |
| 61 | + Py_XDECREF(kwds); |
| 62 | + lval2 = PyLong_AsLong(longobj); |
| 63 | + result = PyLong_FromLong(lval2 + ival + lval); |
| 64 | + Py_INCREF(result); |
| 65 | + return result; |
| 66 | +} |
| 67 | +
|
| 68 | +static PyObject* nc_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { |
| 69 | + return type->tp_alloc(type, 0); |
| 70 | +} |
| 71 | +
|
| 72 | +static int nc_init(PyObject* self, PyObject* args, PyObject* kwds) { |
| 73 | + return 0; |
| 74 | +} |
| 75 | +
|
| 76 | +
|
| 77 | +static struct PyMethodDef nc_methods[] = { |
| 78 | + {"method_varargs", (PyCFunction)nc_method_varargs, METH_VARARGS | METH_KEYWORDS, ""}, |
| 79 | + {NULL, NULL, 0, NULL} |
| 80 | +}; |
| 81 | +
|
| 82 | +static PyTypeObject NativeCustomType = { |
| 83 | + PyVarObject_HEAD_INIT(NULL, 0) |
| 84 | + "NativeCustomType.NativeCustomType", |
| 85 | + sizeof(NativeCustomObject), /* tp_basicsize */ |
| 86 | + 0, /* tp_itemsize */ |
| 87 | + 0, /* tp_dealloc */ |
| 88 | + 0, |
| 89 | + 0, |
| 90 | + 0, |
| 91 | + 0, /* tp_reserved */ |
| 92 | + 0, |
| 93 | + 0, |
| 94 | + 0, |
| 95 | + 0, |
| 96 | + 0, |
| 97 | + 0, |
| 98 | + 0, |
| 99 | + 0, |
| 100 | + 0, |
| 101 | + 0, |
| 102 | + Py_TPFLAGS_DEFAULT, |
| 103 | + 0, |
| 104 | + 0, /* tp_traverse */ |
| 105 | + 0, /* tp_clear */ |
| 106 | + 0, /* tp_richcompare */ |
| 107 | + 0, /* tp_weaklistoffset */ |
| 108 | + 0, /* tp_iter */ |
| 109 | + 0, /* tp_iternext */ |
| 110 | + nc_methods, /* tp_methods */ |
| 111 | + NULL, /* tp_members */ |
| 112 | + 0, /* tp_getset */ |
| 113 | + 0, /* tp_base */ |
| 114 | + 0, /* tp_dict */ |
| 115 | + 0, /* tp_descr_get */ |
| 116 | + 0, /* tp_descr_set */ |
| 117 | + 0, /* tp_dictoffset */ |
| 118 | + (initproc)nc_init, /* tp_int */ |
| 119 | + 0, /* tp_alloc */ |
| 120 | + (newfunc)nc_new, /* tp_new */ |
| 121 | + PyObject_Del, /* tp_free */ |
| 122 | +}; |
| 123 | +
|
| 124 | +static PyModuleDef c_method_module = { |
| 125 | + PyModuleDef_HEAD_INIT, |
| 126 | + "c_method_module", |
| 127 | + "", |
| 128 | + -1, |
| 129 | + NULL, NULL, NULL, NULL, NULL |
| 130 | +}; |
| 131 | +
|
| 132 | +PyMODINIT_FUNC |
| 133 | +PyInit_c_method_module(void) |
| 134 | +{ |
| 135 | + PyObject* m; |
| 136 | +
|
| 137 | + if (PyType_Ready(&NativeCustomType) < 0) |
| 138 | + return NULL; |
| 139 | +
|
| 140 | + m = PyModule_Create(&c_method_module); |
| 141 | + if (m == NULL) |
| 142 | + return NULL; |
| 143 | +
|
| 144 | + PyModule_AddObject(m, "NativeCustomType", (PyObject *)&NativeCustomType); |
| 145 | + return m; |
| 146 | +} |
| 147 | +
|
| 148 | +""" |
| 149 | + |
| 150 | + |
| 151 | +ccompile("c_method_module", code) |
| 152 | +from c_method_module import NativeCustomType |
| 153 | + |
| 154 | +def count(num): |
| 155 | + print("###### NUM: " + str(num)) |
| 156 | + obj = NativeCustomType() |
| 157 | + total = 0 |
| 158 | + for i in range(num): |
| 159 | + total += obj.method_varargs(i, i+1, i+2) |
| 160 | + return total |
| 161 | + |
| 162 | + |
| 163 | +def measure(num): |
| 164 | + result = count(num) |
| 165 | + print("result = " + str(result)) |
| 166 | + |
| 167 | + |
| 168 | +def __benchmark__(num=1000000): |
| 169 | + measure(num) |
0 commit comments