티스토리 뷰

CS

Interpreter VS Compiler

신잼 2021. 11. 26. 00:44

TL;DR

어느 시점에 source code에서 machine code로 compile 되는지에 따라 다르다.

  • interpreter : runtime
  • compiler(AOT) : runtime 전

https://thevaluable.dev/difference-between-compiler-interpreter/

 

실행속도 이식성 컴파일 시간 종류
빠름 별로 느림 go, rust, Haskel
느림 좋음 빠름(bytecode) python, ruby
보통(JIT 구현에 따라 차이) 좋음 빠름(bytecode) pypy, java

Compiler VS Interpreter

Compiler : 번역기

컴파일러(compiler, 순화 용어: 해석기, 번역기)는 특정 프로그래밍 언어로 쓰여 있는 문서를 다른 프로그래밍 언어로 옮기는 언어 번역 프로그램을 말한다.

https://www.youtube.com/watch?v=y6VvxGHCxa4

  • 특정 언어에서 특정 언어로 바꿔 주는 것
  • 원시 코드 -> 목적 코드
  • 원시 코드: 원래의 문서, 소스코드
  • 목적 코드: 주로 하드웨어가 처리하기에 용이한 형태

Compliation

컴파일 과정에는 Tokenization, Parsing 등의 작업이 포함된다.

  • Lexer: 소스 코드(컴퓨터 입장에서는 문자)를 입력받아 여러 개의 토큰으로 변경시킨다.
  • Parser: 토큰을 분석하여 분석 트리(AST)를 구성한다.
  • Code Generator: 기계어 생성

https://towardsdatascience.com/understanding-compilers-for-humans-version-2-157f0edb02dd
https://tomassetti.me/guide-parsing-algorithms-terminology/

Interpreter : 해석기

인터프리터(interpreter, 문화어: 해석기)는 프로그래밍 언어소스 코드를 바로 실행하는 컴퓨터 프로그램 또는 환경을 말한다.

https://www.youtube.com/watch?v=y6VvxGHCxa4

  • 아래 기능 중 적어도 한 가지 기능을 가진 프로그램
  1. 소스 코드를 직접 실행한다.
  2. 소스 코드를 효율적인 다른 중간 코드로 변환하고, 변환한 것을 바로 실행한다
  3. 인터프리터 시스템의 일부인 컴파일러가 만든, 미리 컴파일된 [각주:1] 저장 코드의 실행을 호출한다.

Notes

프로그래밍 언어 목적

Compiled 언어, Interpreted 언어 모두 사람이 읽을 수 있는 Source coded에서 CPU가 실행할 수 있게 Machine code로 바꾸는 것

많은 프로그래밍 언어는 Compiler와 Interpreter를 둘 다 사용한다

  • Source code에서 직접 해석하면서 실행시키는 건 매우 느리기 때문
  • bytecode를 중간에 만들어 놓고 bytecode를 실행시키는 구조

https://qastack.kr/programming/846103/runtime-vs-compile-time

Fully Interpreted Languaes

온전히 바로 Interpreted 되는 언어는 Javascript가 있다.

  • network로 바로 전송이 가능하고 유저의 browser에서 바로 실행이 가능한 이점이 있다.

JIT(Just-In-Time)

  • Hybrid 방식(실행 시점에 인터프리터 방식으로 기계어 코드 생성하여 캐싱)
  • 아직 compile이 되지 않으면 느리지만 compile 되면 빠르다는 장점이 있다.
  • Implementations: java, c#, pypy, v8..

https://beststar-1.tistory.com/3

Python Interpreter

python의 interpreter안에서 compiler가 pyc파일(byte code)을 만들고 python의 runtime engine인 PVM(python virtual machine)에 byte code를 전달한다

  • pyc는 py파일이 import 될 때 생성된다.

http://pytolearn.csd.auth.gr/p0-py/00/langexecmodel.html
https://indianpythonista.wordpress.com/2018/01/04/how-python-runs/
https://velog.io/@jean1042/Back-to-the-basic-Python-interpreter의-작동방식

Python Compliation

  1. Source Code -> Parse Tree
  2. Parse Tree -> AST(Abstract Syntax Tree)
  3. AST -> 제어 흐름 그래프(Control FlowGraph)
  4. CFG -> Byte Code

Other python implementations

https://indianpythonista.wordpress.com/2018/01/04/how-python-runs/


Reference

  1. 이 관점으로 CPU도 machine instructions의 interpreter로 볼 수 있다 [본문으로]
반응형

'CS' 카테고리의 다른 글

[Network] HTTP/0.9 ~ 1.1  (0) 2022.01.03
[Network/OS] Network Socket(IP Socket, WebSocket)  (0) 2021.12.27
[DB]Transaction(Race condition, Isolate level  (0) 2021.11.16
[Network]기본 개념  (0) 2021.10.30
[Network]Cloud 종류  (0) 2021.10.25
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함