Reputation: 387
I'm running a test with timeit and am not understanding what is going on. I've been evaluating 2 raised to a power but every time it says it takes 0.02 ± 0.01 seconds. What am I doing wrong?
Example:
This will take 2.5 mins to execute:
2**10000000
This will return instantly with something like 0.01637562597170472:
timeit.Timer(stmt="2**10000000").timeit()
Upvotes: 2
Views: 88
Reputation: 20224
Additional supplement of @Ryan, If you try a = 2 ** 10000000
, it will return immediately. But then if you try print(a)
, it takes such a long time to execute.
As @Ryan has said, this long time is cost by converting to decimal string. As timeit.Timer(stmt="2**10000000").timeit()
won't print anything, it returns also immediately.
Upvotes: -1
Reputation: 225115
2**10000000
This doesn’t take 2.5 minutes to execute. It takes 2.5 minutes to convert to a decimal string, which is how integers are printed by default. Try (2**10000000) % 10000000
or len(hex(2**10000000))
.
Upvotes: 4