Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3
- def native_power_fuction(number, exponent):
- return pow(number, exponent)
- # pow(1000000, 100000)
- # 4 function calls in 0.295 seconds
- def native_power_operator(number, exponent):
- return number ** exponent
- # native_power_operator(1000000, 100000)
- # 4 function calls in 0.295 seconds
- def power_one(number, exponent):
- accumulator = 1
- while exponent > 0:
- accumulator *= number
- exponent -= 1
- return accumulator
- # power_one(1000000, 100000)
- # 4 function calls in 11.608 seconds
- def power_two(number, exponent):
- accumulator = 1
- for current_exponent in range(exponent, 0, -1):
- accumulator *= number
- return accumulator
- # power_two(1000000, 100000)
- # 4 function calls in 11.576 seconds
- def get_binary_power(source_number, exponent):
- result = 1
- n_exponent = exponent
- a_source = source_number
- while n_exponent != 0:
- # if not "{0:b}".format(n_exponent).endswith("0"):
- # if not str(bin(n_exponent)).endswith("0"):
- # if not n_exponent % 2 == 0:
- if not str(n_exponent).endswith(("0", "2", "4", "6", "8")):
- result *= a_source
- n_exponent >>= 1
- a_source *= a_source
- return result
- # get_binary_power(1000000, 100000)
- # with the format method: 38 function calls in 0.664 seconds
- # with str and bin functions: 38 function calls in 0.667 seconds
- # with modulo: 4 function calls in 0.664 seconds
- # with str and endswith: 21 function calls in 0.664 seconds
- def get_mod_binary_power(source_number, exponent):
- if exponent == 0:
- return 1
- if exponent % 2 == 1:
- return get_mod_binary_power(source_number, exponent - 1) * source_number
- result = get_mod_binary_power(source_number, exponent // 2)
- return result * result
- # get_mod_binary_power(1000000, 100000)
- # 26 function calls (4 primitive calls) in 0.295 seconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement