Last active
January 14, 2023 10:34
-
-
Save ivangeorgiev/23c4de08f2bed235227cf7a1e0f5ffc8 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 time | |
def print_fib(number: int) -> None: | |
def fib(n: int) -> int: | |
if n == 1: | |
return 1 | |
if n == 2: | |
return 1 | |
return fib(n-1) + fib(n-2) | |
print(f'fib({number}) is {fib(number)}') | |
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 multiprocessing | |
import time | |
from fib import print_fib | |
def print_fib_processes(number_1: int, number_2: int): | |
"""Print two fibonacci numbers using separate processes""" | |
worker_1 = multiprocessing.Process(target=print_fib, args=(number_1,)) | |
worker_2 = multiprocessing.Process(target=print_fib, args=(number_2,)) | |
worker_1.start() | |
worker_2.start() | |
worker_1.join() | |
worker_2.join() | |
if __name__ == '__main__': | |
# multiprocessing.freeze_support() | |
start_time = time.time() | |
print_fib_processes(40, 41) | |
end_time = time.time() | |
print(f'Processes completed in {end_time - start_time:.4f} seconds.') |
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 time | |
from typing import Callable | |
from processes_fib import print_fib_processes | |
from sync_fib import print_fib_sync | |
from threaded_fib import print_fib_threaded | |
def print_fib_with(print_fib: Callable): | |
start_time = time.time() | |
print_fib(40, 41) | |
end_time = time.time() | |
print(f'"{print_fib.__name__}" completed in {end_time - start_time:.4f} seconds.') | |
if __name__ == '__main__': | |
print_fib_with(print_fib_sync) | |
print_fib_with(print_fib_threaded) | |
print_fib_with(print_fib_processes) |
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 time | |
from fib import print_fib | |
def print_fib_sync(number_1: int, number_2: int): | |
"""Print two fibonacci numbers synchronously""" | |
print_fib(number_1) | |
print_fib(number_2) | |
if __name__ == '__main__': | |
start_time = time.time() | |
print_fib_sync(40, 41) | |
end_time = time.time() | |
print(f'Synchronous completed in {end_time - start_time:.4f} seconds.') |
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 threading | |
import time | |
from fib import print_fib | |
def print_fib_threaded(number_1: int, number_2: int): | |
"""Print two fibonacci numbers using separate threads""" | |
worker_1 = threading.Thread(target=print_fib, args=(number_1,)) | |
worker_2 = threading.Thread(target=print_fib, args=(number_2,)) | |
worker_1.start() | |
worker_2.start() | |
worker_1.join() | |
worker_2.join() | |
if __name__ == '__main__': | |
start_time = time.time() | |
print_fib_threaded(40, 41) | |
end_time = time.time() | |
print(f'Threaded completed in {end_time - start_time:.4f} seconds.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment