CPU bound
import time
def cpu_bound_func(number:int):
total=1
arrange=range(1,number+1)
#cpu의 자원을 소모하는 반복적인 연산
for i in arrange:
for j in arrange:
for k in arrange:
total+=i+j+k
return total
if __name__=="__main__":
start = time.time()
result=cpu_bound_func(1000)
print("result:",result)
print("실행시간 :", time.time() - start,"seconds")
$ python cpu_bound.py
result: 1501500000001
실행시간 : 67.27040219306946 seconds
단순히 연산작업으로 CPU에 의해 프로그램의 실행 속도가 느려진다.
IO bound
import time
def io_bound_func(number: int):
#사용자에게 입력을 받고 출력
print("값을 입력해주세요:")
input_value=input()
return int(input_value)+100
if __name__=="__main__":
start = time.time()
result=io_bound_func(100)
print("result:",result)
print("실행시간 :", time.time() - start,"seconds")
$ python io_bound.py
값을 입력해주세요:
30
result: 130
실행시간 : 1.8464698791503906 seconds
사용자의 입력을 기다리는 부분으로 인해서 프로그램의 실행 속도가 느려진다.
import requests
import time
def io_http_bound_func():
for i in range(10):
result=requests.get("https://google.com")
return result
if __name__=="__main__":
start = time.time()
result=io_http_bound_func()
print("result:",result)
print("실행시간 :", time.time() - start,"seconds")
$ python io_bound_network.py
result: <Response [200]>
실행시간 : 4.050660610198975 seconds
사용자가 google이 됐고 입력(goole로부터 모든 데이터를 받아와 저장)이 모두 끝날때까지 기다림으로 인해 실행속도가 느려진다.
blocking
블로킹되는 코드를 코루틴,멀티프로세싱,멀티쓰레딩으로 개선시켜 프로그램의 성능을 향상시켜야한다.