In this approach, we'll find out how to most efficiently calculate the value for Grains in Python.
The approaches page lists three idiomatic approaches to this exercise:
Benchmarks
To benchmark the approaches, we wrote a small benchmark application using the timeit library.
bit shifting square 64: 1.2760140001773835e-07
bit shifting total 64: 6.096410000463947e-08
exponentiation square 64: 4.122742000035942e-07
exponentiation total 64: 6.094090000260621e-08
pow square 64: 4.2468130000634117e-07
pow total 64: 3.1965399999171494e-07
- Bit shifting was the fastest for
square
. - Bit shifting and exponentiation were about the same for
total
. - Exponentiation and
pow
were about the same forsquare
. -
pow
was significantly the slowest fortotal
.
Benchmarks were also done to substitute if number not in range(1, 65):
for if number < 1 or number > 64:
.
bit shifting square 64: 2.708769000018947e-07
exponentiation square 64: 5.56936200009659e-07
pow square 64: 5.738279999932274e-07
Using if number not in range(1, 65):
was over 125
nanoseconds longer than using if number < 1 or number > 64:
for all approaches.
4th Dec 2024
·
Found it useful?