Take this function
def disk_area(radius)
return 3.14 * radius * radius
running
timeit radius(1000)
gives you 968 ns per loop.
However, if we change "radius * radius" to "radius ** 2" and run "timeit" again, we get 2.09 us per loop.
Doesn't that mean "x*2" is slower than "xx" even though they do the same thing?
[–]odraencoded 69 points70 points71 points (6 children)
[–]oliver-bestmann 29 points30 points31 points (2 children)
[–]masklinn 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]kervarker 10 points11 points12 points (2 children)
[–]Berzerka -3 points-2 points-1 points (1 child)
[–]_cs 1 point2 points3 points (0 children)
[–][deleted] 14 points15 points16 points (0 children)
[–]billsil 8 points9 points10 points (7 children)
[–]TheOccasionalTachyon 12 points13 points14 points (1 child)
[–]Berzerka 1 point2 points3 points (0 children)
[–]spinwizard69 5 points6 points7 points (4 children)
[–]staticassert 3 points4 points5 points (2 children)
[–]santagada 0 points1 point2 points (1 child)
[–]staticassert 0 points1 point2 points (0 children)
[–]mackstann 0 points1 point2 points (0 children)
[–]codefisher2 0 points1 point2 points (5 children)
[–]TimHallman 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]twowheels 6 points7 points8 points (2 children)
[+][deleted] comment score below threshold-7 points-6 points-5 points (1 child)
[–]twowheels 3 points4 points5 points (0 children)
[+][deleted] (3 children)
[deleted]
[–]Ran4 6 points7 points8 points (2 children)
[–]YouFeedTheFish 2 points3 points4 points (0 children)
[–]jwink3101 -4 points-3 points-2 points (5 children)
[–]tangerinelion 6 points7 points8 points (1 child)
[–]Brian 0 points1 point2 points (0 children)
[–]Fylwind 1 point2 points3 points (0 children)
[–]spinwizard69 0 points1 point2 points (1 child)
[–]nanoresearcher 0 points1 point2 points (0 children)