






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Expression Timing (100k iterations) x * x 3.87 ms x ** 2 80.97 ms
math.pow(x, 2) 83.60 ms
0 LOAD_FAST 0 (x) 2 LOAD_FAST 0 (x) 4 BINARY_MULTIPLY 6 RETURN_VALUE
0 LOAD_GLOBAL 0 (math) 2 LOAD_ATTR 1 (pow) 4 LOAD_FAST 0 (x) 6 LOAD_CONST 1 (2) 8 CALL_FUNCTION 2 10 RETURN_VALUE
if (((Py_SIZE(a) ^ Py_SIZE(b)) < 0) && z) { _PyLong_Negate(&z); if (z == NULL) return NULL; } return (PyObject *)z; }
case TARGET(BINARY_MULTIPLY): { PyObject *right = POP(); PyObject *left = TOP(); PyObject *res = PyNumber_Multiply(left, right); Py_DECREF(left); Py_DECREF(right); SET_TOP(res); if (res == NULL) goto error; DISPATCH(); }
if (Py_SIZE(b) < 0) { /* if exponent is negative */ if (c) { PyErr_SetString(PyExc_ValueError, "pow() 2nd argument " "cannot be negative when 3rd argument
specified"); goto Error; } else { /* else return a float. This works because we know that this calls float_pow() which converts its arguments to double. */ Py_DECREF(a); Py_DECREF(b); return PyFloat_Type.tp_as_number->nb_power(v, w, x); } }
case TARGET(BINARY_POWER): { PyObject *exp = POP(); PyObject *base = TOP(); PyObject *res = PyNumber_Power(base, exp, Py_None); Py_DECREF(base); Py_DECREF(exp); SET_TOP(res); if (res == NULL) goto error; DISPATCH(); }
#define REDUCE(X)
1
exponent = lambda base, power: base ** power
def generate_mult_func(n): mult_steps = '*'.join(['q'] * n) func_string = f'lambda q: {mult_steps}' # Keep this so we can print later return eval(func_string), func_string
multiply, func_string = generate_mult_func(power)
lambda q: qqq*q 3
base = 2 power = 2
Starting speeds: Multiply time 11.83 ms Exponent time 86.52 ms math.pow time 73.90 ms
Crossover found in 1.2 s: Base, power 2, 15 Multiply time 110.09 ms Exponent time 108.20 ms math.pow time 79.82 ms Multiply func lambda q: qqqqqqqqqqqqqqq
Power multiply exponent math.pow
4