Created
December 22, 2021 09:24
-
-
Save bdejong/7292e59a7a426de1caa69ad8c5f58747 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def get_digits_string_map(num): | |
return list(map(int, str(num))) | |
def get_digits_string_list(num): | |
return [int(char) for char in str(num)] | |
def get_digits(num): | |
return [(num // (10 ** i)) % 10 for i in range(int(math.log10(num)), -1, -1)] | |
def get_digits_2(num): | |
result = [] | |
if num == 0: | |
result.append(0) | |
while num: | |
num, d = divmod(num, 10) | |
result.append(d) | |
result.reverse() | |
return result | |
def get_digits_3(num): | |
result = [] | |
if num == 0: | |
result.append(0) | |
while num: | |
d = num % 10 | |
num = num // 10 | |
result.append(d) | |
result.reverse() | |
return result | |
def benchmark(): | |
import timeit | |
def time_func(function): | |
print(function.__name__, timeit.timeit(lambda: function(123456789123456789), number=1000000)) | |
time_func(get_digits_string_map) | |
time_func(get_digits_string_list) | |
time_func(get_digits) | |
time_func(get_digits_2) | |
time_func(get_digits_3) | |
if __name__ == "__main__": | |
benchmark() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment