Last active
October 23, 2017 03:48
-
-
Save devlights/c2cc07b6ff2517e7d88cde6a998922e6 to your computer and use it in GitHub Desktop.
[python] 最大公約数求める
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 functools | |
| import time | |
| def timetrace(func): | |
| @functools.wraps(func) | |
| def _wrapper(*args, **kwargs): | |
| start = time.time() | |
| results = func(*args, **kwargs) | |
| end = time.time() | |
| print(f'Took {end - start} sec') | |
| return results | |
| return _wrapper | |
| @timetrace | |
| def gcd(pair): | |
| x, y = pair | |
| low = min(x, y) | |
| for i in range(low, 0, -1): | |
| if x % i == 0 and y % i == 0: | |
| return i | |
| if __name__ == '__main__': | |
| import concurrent | |
| numbers = [ | |
| (1963309, 2265973), | |
| (2030677, 3814172), | |
| (1551645, 2229620), | |
| (2039045, 2020802) | |
| ] | |
| pool = concurrent.futures.ProcessPoolExecutor(max_workers=4) | |
| results = list(pool.map(gcd, numbers)) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
大きな数値を渡すと、それなりに時間がかかる処理なのでちょうどいい。