a // 1000 % 10000看到有不少用把數轉成字串再截取了,比如 int(str(a)[-7:-3])。行是行,但是當數很大時,轉成十進位制字串表示很慢的,看:$ python -m timeit -s "a = 2 ** 10000" "a // 1000 % 10000"100000 loops, best of 3: 14.6 usec per loop$ python -m timeit -s "a = 2 ** 10000" "int(str(a)[-7:-3])"1000 loops, best of 3: 444 usec per loop$ python -m timeit -s "a = 2 ** 100000" "a // 1000 % 10000"10000 loops, best of 3: 141 usec per loop$ python -m timeit -s "a = 2 ** 100000" "int(str(a)[-7:-3])"10 loops, best of 3: 43.2 msec per loop
a // 1000 % 10000看到有不少用把數轉成字串再截取了,比如 int(str(a)[-7:-3])。行是行,但是當數很大時,轉成十進位制字串表示很慢的,看:$ python -m timeit -s "a = 2 ** 10000" "a // 1000 % 10000"100000 loops, best of 3: 14.6 usec per loop$ python -m timeit -s "a = 2 ** 10000" "int(str(a)[-7:-3])"1000 loops, best of 3: 444 usec per loop$ python -m timeit -s "a = 2 ** 100000" "a // 1000 % 10000"10000 loops, best of 3: 141 usec per loop$ python -m timeit -s "a = 2 ** 100000" "int(str(a)[-7:-3])"10 loops, best of 3: 43.2 msec per loop