Skip to main content

Java는 C보다 성능이 안나온다?? JIT의 힘

'Effective Java' 8장을 보면 이런 문구가 있다.

그러나 네이티브 메서드를 통해 성능을 개선하는 것은 추천하고 싶지 않다.

통상, 네이티브 메서드(c, c++로 만들어진 API를 호출)는 성능향상을 위해 사용한다고 알고 있다.
그렇다면 네이티브 메서드를 통해 왜 성능 개선을 하지 말라는 것일까?

먼저 'Java Performance' 라는 위키를 본다.



흔히 자바는 느리다고 생각한다. 왜냐하면 JVM 위에서 돌아가기 때문에 직접적으로 CPU를 컨트롤하는 C, C++에 비해 느리기 때문이다.
맞는말이다. 하지만 현재의 JVM에는 틀린 말이다.
왜나하면 JIT (Just-In-Time Compilation) 을 통해 드라마틱한 성능향상이 있었기 때문이다. (1997/1998 Java 1.1에서 첫 소개)
이전 블로깅 글 ('Android 가 어찌 이리 빠른 성능을 낼 수 있는가?' ) 에 JIT을 간과했을 때 생기는 부끄러움이 생기는 것을 경험해봤다.

* JIT 보다가 흥미로운 점 발견. compile tpye이 -client, -server로 나뉘는데 말그대로 -client는 어플리케이션 용도, -server는 서버 용도이다.
client는 GUI 우선순위를 좀 더 두고, server는 순전히 성능에만 초점을 둔 각자 다른 binary file을 사용한다고 함. 
* JIT 컴파일러는 는 인터프리터, 컴파일 두가지 속성을 다 가지고 있다.
필요할 때만 .class파일을 읽는게 아니라, 자주 사용하는 .class의 경우 machine code를 생성해 두고 컴파일된 형태로 계속 사용한다. (재생성하지 않고 재사용한다.)


'Java Performance' 위키 페이지에는 다음과 같은 글이 있다.

Java is in some cases equal to C++ on low-level and numeric benchmarks

게임 '퀘이크 2' 의 게임엔진도 Java로 포팅하여 구현했다고 한다. (게임엔진에는 정수,실수 등 low-level 연산이 많다.)

그리고 C 스타일과 자바를 비교도 있다.

* C 스타일의 포인터 사용은, 포인터를 지원하는 언어로 최적화를 방해 할 수 있습니다.
C ++ 컴파일러는 항상 객체 포인터에 의한 코드의 특정 블록에서 수정 될 것인지 알 수 없습니다. escape analysis 기술의 사용은 C ++에서 제한적입니다.
* 자바의 instance method 접근 방법은, C++의 별도의 Virtual-Table lookup을 하는 virtual method 접근법보다 빠르다. 하지만 C++의 non-virtual-method 들은 v-table을 사용하지 않기 때문에 특별한 병목이 없을테고, 고로 이 부분에서의 자바,C++의 성능은 비슷하다.

  • C-style pointer usage can hinder optimization in languages that support pointers,
  • The use of escape analysis techniques is limited in C++, for example, because a C++ compiler does not always know if an object will be modified in a particular block of code due to pointers,[note 1]
  • Java can access derived instance methods faster than C++ can access derived virtual methods due to C++'s extra Virtual-Table look-up. However, non-virtual methods in C++ do not suffer from V-Table performance bottlenecks, and thus exhibit performance similar to that of Java.


http://blog.naver.com/sungback/80019601491 에서 발췌한 '제임스 고슬링'의 말.

제임스 고슬링의 일화

번역하기 귀찮아서 원문으로 싣습니다. 간단히, -server 옵션만으로도 GCC 로 충분히 최적화된 C/C++ 어플리케이션에 필적한 성능이 나온다는 이야기입니다.
There was a funny incident at a recent developer event where some folks had a booth where they where demo-ing a high end industrial strength C compiler and had a benchmark that they had transliterated into Java. They were comparing their compiler to GCC and Java. GCC was running at about 2/3 the performance of this high end compiler; the Java version was running at about 2/3 the performance of the GCC version. Folks were gathered around the booth and someone noticed that the script they were using to run the Java version didn’t have optimisation turned on. A few seconds with vi to add the “-server” switch and Java’s performance jumped up to match the fancy C compiler. This got the pro-GCC crowd all excited, so a bunch of them started fiddling with its command line switches. They got a bit of improvement, but not much (the original selection had been pretty good).
[출처] 자바 성능|작성자 메멘토

참고
* 자바 성능 위키
* 자바로 포팅한 퀘이크2 게임 엔진
* JIT 위키

Comments