본문 바로가기

Others/Information

The Inefficiency of C++, Fact or Fiction?

link: http://caxapa.ru/thumbs/213960/iar_fact.pdf

 

일반적으로 임베디드 소프트웨어를 개발하는 개발자들 사이에서 널리 알려진 진실은 코드 크기 및 속도의 관점에서 C언어 보다 C++이 나쁘다고 합니다.

  

위의 링크는 이에 대한 허와 실에 대해 이야기한 문서인데 한번쯤 읽어보면 좋겠다는 생각이 듭니다. 

 

읽어봤더니 결론은 아래와 같습니다. 참고로 여기서의 비용은 코드의 크기에 대한 비용을 말합니다.

 

아시겠지만 코드 최적화는 크기에 대한 최적화와 속도에 대한 최적화로 나뉘며, 일반적으로 이 둘의 관계는 trade-off 를 유지하기 마련이죠.

 

평가 지수:

- FREE: C와 C++은 별 차이 없음.

- CHEAP: C++이 C 보다 약간 더 비용이 듬.

- EXPENSIVE: C++이 C 보다 훨씬 더 많은 비용이 듬.

 

평가 항목:
1. encapsulation/classes
2. namespaces
3. inlining
4. operator overloading
5. constructors/destructors
6. references
7. inheritance and virtual functions
8. templates
9. STL(Standard Template Library)
10. RTTI
11. exceptions

 

평가 결과:

encapsulation/classes : the cost of function calls and use of pointers are FREE.

 

namespaces : Namespace has no cost associated with it: FREE.

 

inlining : Inlining in C++ is essential for good code generation and the cost is therefore FREE.

 

operator overloading : If used in a natural way, such as using + for imaginary numbers and concatenation operations, operator overloading is a powerful way of simplifying code writing and it is FREE.

 

constructors/destructors : Constructors and destructors are essentially FREE except for the actual code they contain.

 

references : References have the same cost as passing a pointer, and are FREE.

 

inheritance and virtual functions : Using virtual functions does come with a price.  However, the code price paid is still CHEAP, and for many cases can be considered as almost FREE. In addition, virtual functions provides huge advantages for future code maintenance and product development.

 

templates : Templates can be FREE, CHEAP, or EXPENSIVE.

 

STL: Using STL reduces implementation time, compared with hand crafting, and the result is likely to work as intended but at a considerable code and data cost. Using the STL library is EXPENSIVE.

 

RTTI: It requires the literal names of all classes to be part of the application binary, and also adds extra code, it is EXPENSIVE.

exceptions:
A lot of extra code is needed to implement the exception mechanism, it is EXPENSIVE.