[컴][파이썬] CPython 과 Cython


CPython 이랑 Cython 은 이름이 비슷하게 생겨서 나는 같은 것 인줄 알았다.^^;;;;
그런데, 찾아보니 CPython 은 우리가 기본적으로 쓰는 python 의 interpreter 를 얘기하는 것이었고, Cython 은 내가 알고 있던, python 에 추가적으로 static type 등을 정의할 수 있게 해주는 또 하나의 language 였다.
  • CPython : interpreter
  • Cython : programming language

아래는 wiki 의 내용을 단순히 요약한 내용이다. 자세한 사항은 ref. 1, ref. 2 를 참고하자.

CPython

C로 작성된 python programming language 의 구현(implementation)이다.

우리가 기본적으로 쓰고 있는 것 python 의 interpreter 가 이 CPython 이다.

CPython 은 bytecode interpreter 이다.
C 같은 외부 함수에 접근할 수 있는 interface(foreign function interface)를 갖고 있다. 이 interface 를 통해서 bindings 을 작성할 수 있다.


Cython

Cython 은 python 의 superset(python + 추가요소)
Cython 은 CPython extension module 을 만들어준다.

추가요소
  • c/c++ 를 호출하는 함수 interface 와
  • static type 의 정의
    • routine parameter
    • 지역 변수들(local variables)
    • 함수의 결과들(subroutine results)
    • class 의 attributes

확장자

.pyx 확장자(extension)을 갖는다.

data type

python 의 primitive type 을 쓸 수도 있고, c types 를 정의할 수도 있다.

CPython 과 실행방법의 차이

똑같은 python code 여도 좀 더 적은 computing resource (core memory, processing cycles)를 사용한다. 왜냐하면, CPython 과 Cython 의 실행모델(exectuion model)차이 때문이다.

일반적은 python code 를 실행하면, CPython의 VM 에 의해 load 되고, 실행되면, runtime 과 program 모두가 computing resource 를 사용한다.

Cython 은 C code 로 컴파일되고, 이게 또 기계어(compiled code) 로 compile 된다. 그래서 VM 은 program 이 load 될 때만 잠깐 사용된다.

C 가 결국 intermediate language 가 되고, 결국 performance 는 c compiler 에 의해 좌우된다.

Cython 은 그래서 numerical computing 에 사용되기도 하며,
많은 c/c++의 library 들이 Pythonic binding 을 제공하는 경우에도 많이 쓰인다.


Reference

  1. http://en.wikipedia.org/wiki/Cython

댓글 없음:

댓글 쓰기