Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

* vs ** for a power of 2 operation [duplicate]

So I've been constantly seeing people write code, when they wish to see the squared version of a given value, they will write out x*x instead of x**2. Is there that big of an efficiency overlap between these two points, that the given function, in python, is not just used, or is it just a stylistic point? I would much rather use the ** operator, but if it will cause a huge misstep, should I be doing the operation a billion times, excessive I know, I'd kind of like to know. Also if I am mistaken in the order of magnitude for operations that one takes over the other I'd also like to be corrected on that. ie if ** is more efficient than x*x then I'd like to know why as well.

like image 650
L.P. Avatar asked Feb 09 '23 18:02

L.P.


2 Answers

I do not agree with g.d.d.c, multiplication is much faster !

"""Evaluating the difference in execution time between n*n and n**2"""

from time import time

n = 2
t = time()
for i in range(5000000):
    n**2
print("Time for n**2:\t%0.3f" % (time()-t))
t = time()
for i in range(5000000):
    n*n
print("Time for n*n:\t%0.3f" % (time()-t))


def test(n):
    """
    Difference in execution time between n*n and n**2
    within function scope.
    """
    t = time()
    for i in range(5000000):
        n**2
    print("Time for n**2:\t%0.3f" % (time()-t))
    t = time()
    for i in range(5000000):
        n*n
    print("Time for n*n:\t%0.3f" % (time()-t))

test(n)

Results :

Time for n**2: 2.324496030807495
Time for n*n:  0.5879969596862793
Time for n**2: 2.0771241188049316
Time for n*n:  0.2894318103790283

You can see that multiplication is about 4 times faster outside of a function, and 7 times faster in the function. I cannot explain the difference between those two tests, and I am not sure about the difference between n*n and n**2, but it might be related with the fact that Python is an interpreted language, and the processing of the latter takes more time, even if the processor operations are very similar, as g.d.d.c demonstrates.

like image 144
Labo Avatar answered Feb 11 '23 09:02

Labo


Realistically, the two are probably very similar in total cost:

>>> def s1(x):
...   return x * x
...
>>>
>>> def s2(x):
...   return x ** 2
...
>>>
>>> from dis import dis
>>>
>>> dis(s1)
  2           0 LOAD_FAST                0 (x)
              3 LOAD_FAST                0 (x)
              6 BINARY_MULTIPLY
              7 RETURN_VALUE
>>> dis(s2)
  2           0 LOAD_FAST                0 (x)
              3 LOAD_CONST               1 (2)
              6 BINARY_POWER
              7 RETURN_VALUE
>>>

I think you're probably prematurely optimizing, even for millions or billions of iterations. Unless you've identified this as a bottleneck, just use what's most idiomatic for you.

And, for completeness, the timeit results:

>>> timeit.Timer("s1(10)", 'from __main__ import s1').timeit(100000)
0.0186597650628606
>>> timeit.Timer("s2(10)", 'from __main__ import s2').timeit(100000)
0.018789616358585448

Which appears to show that x * x is ever so slightly faster in 100000 iterations.

like image 23
g.d.d.c Avatar answered Feb 11 '23 07:02

g.d.d.c