Concurrent Programming Project - Questions/Discussion

So there was no forum post for this project, and there was no tag for it either (used one that at least says thread lol). This post is for the Concurrent Programming project in the AI Engineering course, I just want to share my code so other ppl can see it and compare/etc and have a place to ask questions.

My main question that convinced my to post this is, why the heck do the list comprehension versions take so much longer than the longer format versions?? I don’t really get it or how they can go under less than 1 second. Doesn’t make sense to me hope one you get it or can explain at least lol. Adding full code below (I have both the list comprehension and etc code in the functions, you can just comment them out to try the different versions however you want):

import time import threading import asyncio from multiprocessing import Process # It is waaaaaayyyyy faster doing it in long format rather than in a list comprehension, idk y but figure that out when u can. def cal_average(num): # Average function used for sequential programming, threading, and multiprocessing sum_num = 0 for t in num: sum_num = sum_num + t avg = sum_num / len(num) time.sleep(1) print('Place holder') return avg def main_sequential(list1, list2, list3): # Main wrapper for sequential example s = time.perf_counter() # your code goes here lists = [list1, list2, list3] for i in range(len(lists)): cal_average(lists[i]) elapsed = time.perf_counter() - s print("Sequential Programming Elapsed Time: " + str(elapsed) + " seconds") async def cal_average_async(num): # Average function used for asynchronous programming only ( needs await asyncio.sleep() ) sum_num = 0 for t in num: sum_num = sum_num + t avg = sum_num / len(num) await asyncio.sleep(1) print('Place holder') return avg async def main_async(list1, list2, list3): # Main wrapper for asynchronous example s = time.perf_counter() # your code goes here tasks = [cal_average_async(list1), cal_average_async(list2), cal_average_async(list3)] await asyncio.gather(*tasks) elapsed = time.perf_counter() - s print("Asynchronous Programming Elapsed Time: " + str(elapsed) + " seconds") def main_threading(list1, list2, list3): # Main wrapper for threading example s = time.perf_counter() # your code goes here lists = [list1, list2, list3] # threads = [] threads = [threading.Thread(target=cal_average, args=(lists[i],)) for i in range(len(lists))] # for i in range(len(lists)): # x = threading.Thread(target=cal_average, args=(lists[i],)) # x.start() for t in threads: t.start() for t in threads: t.join() elapsed = time.perf_counter() - s print("Threading Elapsed Time: " + str(elapsed) + " seconds") def main_multiprocessing(list1, list2, list3): # Main wrapper for multiprocessing example s = time.perf_counter() # your code goes here lists = [list1, list2, list3] processes = [] # processes = [Process(target=cal_average, args=(lists[i],)) for i in range(len(lists))] for i in range(len(lists)): p = Process(target=cal_average, args=(lists[i],)) p.start() # for p in processes: # p.start() for p in processes: p.join() elapsed = time.perf_counter() - s print("Multiprocessing Elapsed Time: " + str(elapsed) + " seconds") if __name__ == '__main__': # Need to use this if-statement so multiprocessing doesn't cause an infinite loop l1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Three lists we are trying to calculate average on l2 = [2, 4, 6, 8, 10] l3 = [1, 3, 5, 7, 9, 11] main_sequential(l1, l2, l3) loop = asyncio.get_event_loop() loop.run_until_complete(main_async(l1, l2, l3)) main_threading(l1, l2, l3) main_multiprocessing(l1, l2, l3)