![]() To see this, here's yet another method for computing 2 to the 38th power, along with it's disassembled form: > import math However, not all function calls are created equal, and the number of lines of bytecodes is not necessarily indicative of execution time. Given Python's reputation for having expensive function calls, it seems reasonable for pow(2,38) to be an order of magnitude slower than 2**38. ![]() In particular, pow(2,38) disassembled form included the bytecode CALL_FUNCTION, while 2**38 does not. In other words, we can turn each line of code into its underlying set of fundamental instructions: > import dis To see why 2**38 is faster, we can use the dis module to dissemble each operations into bytecode. That's a big difference! Why 2**38 is faster than pow(2,38) In other words, 2**38 takes about one-seventieth of a second, while pow(2,38) takes more than one-third of a second. Using the timeit module, we can find the average execution time when each operation is repeated one million times: > import timeit To see this, consider a simple timing experiment. Per Python's documentation, we could instead write the computation in either of the following ways: > 2**38Įven though 2**38 and pow(2,38) produce the same number, through, one of them is working harder behind the scenes. If you've used a Texas Instruments graphing calculators or a mathematical typesetting language like LaTeX, you might instinctively try the following in Python: > 2^38Īfter a quick Google search, it's clear the caret ( ^) denotes "bitwise exclusive or", which isn't at all what we wanted. Suppose you want to calculate a large number like 2 to the 38th power. Note: This blog post is a companion to our Episode 2 Teaser. A podcasted journey from learner to developerĮpisode 2 Teaser Companion Blog Post: The Magic Behind Integer Arithmetic in Python Monday, March 19th, 2018 at 11:00 PM
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |