|
| 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) |
0 commit comments