프로그래밍/Python

파이썬 divmod( ) 와 %, // 차이

O'bin 2023. 1. 5. 16:12

   divmod( ) 

두 개의 숫자를 인자로 받아, 나눠지는 수를 나누는 수로 나눈 몫과 나머지를 튜플 형식으로 반환하는 함수

 

>> divmod(num1, num2)

( num1//num2, num1%num2 )

 

 

 

   divmod( ) 와 %, // 차이는?

divmod( )보다 %, // 연산 수행 시 더 많은 Opcode(명령코드) 실행

=> 인자값의 크기가 그렇게 크지 않은 경우 %, // 연산이 더 빠르지만,

     인자값의 크기가 매우 커지면 divmod( ) 연산이 더 빠르다.

 

 

 

내용 참고 :

https://stackoverflow.com/questions/30079879/is-divmod-faster-than-using-the-and-operators

 

Is divmod() faster than using the % and // operators?

I remember from assembly that integer division instructions yield both the quotient and remainder. So, in python will the built-in divmod() function be better performance-wise than using the % and //

stackoverflow.com