본문 바로가기

Python/고급

CPU Bound(1) - Single process

CPU 연산 - 단일 프로세스

 

import time


def cpu_bound(number):
    return sum(i * i for i in range(number))


def find_sums(numbers):
    result = []
    for number in numbers:
        result.append(cpu_bound(number))

    return result


def main():
    numbers = [3_000_000 + x for x in range(30)]

    # 확인
    # print(numbers)
   
    start_time = time.time()
    total = find_sums(numbers)

	print()
    print(f"Total list : {total}")
    print(f"Sum : {sum(total)}")

    duration = time.time() - start_time

    print()
    print(f"Duration : {duration} seconds")

if __name__ == "__main__":
    main()

------------------------------------[result]

Total list : [8999995500000500000, 9000004500000500000, 9000013500006500001, 9000022500018500005, 9000031500036500014, 9000040500060500030, 9000049500090500055, 9000058500126500091, 9000067500168500140, 9000076500216500204, 9000085500270500285, 9000094500330500385, 9000103500396500506, 9000112500468500650, 9000121500546500819, 9000130500630501015, 9000139500720501240, 9000148500816501496, 9000157500918501785, 9000166501026502109, 9000175501140502470, 9000184501260502870, 9000193501386503311, 9000202501518503795, 9000211501656504324, 9000220501800504900, 9000229501950505525, 9000238502106506201, 9000247502268506930, 9000256502436507714]
Sum : 270003780024375058870

Duration : 9.115996837615967 seconds

'Python > 고급' 카테고리의 다른 글

CPU Bound(2) - Multi-process  (0) 2021.05.20
[Python] I/O Bound(2) - Asyncio  (0) 2021.05.20
[Python] I/O Bound(2) - Asyncio basic  (0) 2021.05.20
[Python] I/O Bound(2) - Multiproecessing  (0) 2021.05.20
[Python] I/O Bound(2) - Multithreading  (0) 2021.05.20