Contents - Cultural View

Contents - Cultural View Contents - Cultural View

culturalview.com
from culturalview.com More from this publisher
14.07.2013 Views

Comparison of Java and C++ 90 Templates are Turing-complete (see template metaprogramming). Generics are not Turing-complete. Miscellaneous • Java and C++ use different techniques for splitting up code in multiple source files. Java uses a package system that dictates the file name and path for all program definitions. In Java, the compiler imports the executable class files. C++ uses a header file source code inclusion system for sharing declarations between source files. (See Comparison of imports and includes.) • Compiled Java code files are generally smaller than code files in C++ as Java bytecode is usually more compact than native machine code and Java programs are never statically linked. • C++ compilation features an additional textual preprocessing phase, while Java does not. Thus some users add a preprocessing phase to their build process for better support of conditional compilation. • In both languages, arrays have a fixed size. In Java, arrays are first-class objects, while in C++ they are merely a continuous run of their base objects, often referred to using a pointer to their first element and an optional length. In Java, arrays are bounds-checked and know their length, while in C++ you can treat any subsequence as an array in its own right. Both C++ and Java provide container classes (std::vector and java.util.ArrayList respectively) which are resizable and store their size. • Java's division and modulus operators are well defined to truncate to zero. C++ does not specify whether or not these operators truncate to zero or "truncate to -infinity". -3/2 will always be -1 in Java, but a C++ compiler may return either -1 or -2, depending on the platform. C99 defines division in the same fashion as Java. Both languages guarantee (where a and b are integer types) that (a/b)*b + (a%b) == a for all a and b (b != 0). The C++ version will sometimes be faster, as it is allowed to pick whichever truncation mode is native to the processor. • The sizes of integer types is defined in Java (int is 32-bit, long is 64-bit), while in C++ the size of integers and pointers is compiler and ABI dependent within given constraints. Thus, carefully-written C++ code can take advantage of the 64-bit processor's capabilities while still functioning properly on 32-bit processors. However, care must be taken to write the C++ program in a portable manner. In contrast, Java's fixed integer sizes mean that programmer error in this regard shouldn't be possible. This may incur a performance penalty since Java code cannot run using an arbitrary processor's word size. Performance In addition to running a compiled Java program, computers running Java applications must also run the Java Virtual Machine JVM, while compiled C++ programs can be run without external applications. Early versions of Java were significantly outperformed by statically compiled languages such as C++. This is because the program statements of these two closely related languages may compile to a few machine instructions with C++, while compiling into several byte codes involving several machine instructions each when interpreted by a JVM. For example: Java/C++ statement C++ generated code (x86) Java generated byte vector[i]++; mov edx,[ebp+4h] mov eax,[ebp+1Ch] inc dword ptr [edx+eax*4] aload_1 iload_2 dup2 iaload iconst_1 While this may still be the case for embedded systems because of the requirement for a small footprint, it is frequently argued that advances in just in time (JIT) compiler technology for long-running server and desktop Java processes will close the performance gap. iadd iastore code

Comparison of Java and C++ 91 Several studies of mostly numerical benchmarks argue that Java could potentially be faster than C++ in some circumstances, [7] [8] [9] . However, it was shown that numerical (micro-)benchmarks are not appropriate for evaluation of languages as compiler is able to optimize both cases equally, or eliminate benchmarked code entirely. [10] [11] [12] If referring to a real world program, Java would suffer because of number of reasons: [13] [14] [15] • All objects are allocated on the heap. For functions using small objects this can result in huge performance degradation as stack allocation costs essentially zero. • Methods are by-default virtual. This increases memory usage as much as several times for small objects because of virtual function tables. Also, it induces performance penalty, because JIT compiler has to do additional optimization passes even for de-virtualization of small functions. • A lot of casting required even using standard containers induces performance penalty as it is needed to walk through entire inheritance hierarchy. • Virtual Java Machine Increases memory usage even further, thus reducing memory locality and increasing chances of cache misses and therefore slowdown of the program. • Lack of access to low level details does not allow developer to better optimize the program where the compiler is unable to do so. [16] It is argued, however, that compared to Java, C++ also has a number of downsides: • Pointers make optimization difficult since they may point to arbitrary data. However this is obsoleted as new compilers introduced strict-aliasing rule [17] and because of support of the C99 keyword restrict. [18] • Java garbage collection may have better cache coherence than usual usage of malloc/new for memory allocation as its allocations are generally made sequentially. Nevertheless, arguments exist that both allocators equally fragment the heap and no one exhibits better cache locality. • Run-time compilation can potentially use additional information available at run-time to optimise code more effectively, such as knowing what processor the code will be executed on. However this claim is effectively made obsolete as most state-of-the-art C++ compilers generate multiple code paths to employ full computational abilities of the given system [19] . Proprietary Control C++ is not a trademark of any company or organization and is not owned by any individual. [20] Java is a trademark of Sun Microsystems, which is now owned by Oracle. [21] The C++ language is defined by ISO/IEC 14882, an ISO standard, which is published by the ISO/IEC JTC1/SC22/WG21 committee. The Java language is defined by the Java Language Specification[36], a book which is published by Sun (now Oracle). External references • Java and C++ Memory Management [22] — an exhaustive publication about object-oriented memory management that compares Java and C++ in terms of Memory Modeling. • How Java Differs from C [23] — excerpt from Java in a Nutshell by David Flanagan • Java vs. C++ resource management comparison [24] - Comprehensive paper with examples • Java vs C performance... again... [25] - In-depth discussion of differences between Java and C / C++ with regard to performance

Comparison of Java and C++ 90<br />

Templates are Turing-complete (see template metaprogramming). Generics are not Turing-complete.<br />

Miscellaneous<br />

• Java and C++ use different techniques for splitting up code in multiple source files. Java uses a package system<br />

that dictates the file name and path for all program definitions. In Java, the compiler imports the executable class<br />

files. C++ uses a header file source code inclusion system for sharing declarations between source files. (See<br />

Comparison of imports and includes.)<br />

• Compiled Java code files are generally smaller than code files in C++ as Java bytecode is usually more compact<br />

than native machine code and Java programs are never statically linked.<br />

• C++ compilation features an additional textual preprocessing phase, while Java does not. Thus some users add a<br />

preprocessing phase to their build process for better support of conditional compilation.<br />

• In both languages, arrays have a fixed size. In Java, arrays are first-class objects, while in C++ they are merely a<br />

continuous run of their base objects, often referred to using a pointer to their first element and an optional length.<br />

In Java, arrays are bounds-checked and know their length, while in C++ you can treat any subsequence as an<br />

array in its own right. Both C++ and Java provide container classes (std::vector and java.util.ArrayList<br />

respectively) which are resizable and store their size.<br />

• Java's division and modulus operators are well defined to truncate to zero. C++ does not specify whether or not<br />

these operators truncate to zero or "truncate to -infinity". -3/2 will always be -1 in Java, but a C++ compiler may<br />

return either -1 or -2, depending on the platform. C99 defines division in the same fashion as Java. Both<br />

languages guarantee (where a and b are integer types) that (a/b)*b + (a%b) == a for all a and b (b != 0). The C++<br />

version will sometimes be faster, as it is allowed to pick whichever truncation mode is native to the processor.<br />

• The sizes of integer types is defined in Java (int is 32-bit, long is 64-bit), while in C++ the size of integers and<br />

pointers is compiler and ABI dependent within given constraints. Thus, carefully-written C++ code can take<br />

advantage of the 64-bit processor's capabilities while still functioning properly on 32-bit processors. However,<br />

care must be taken to write the C++ program in a portable manner. In contrast, Java's fixed integer sizes mean that<br />

programmer error in this regard shouldn't be possible. This may incur a performance penalty since Java code<br />

cannot run using an arbitrary processor's word size.<br />

Performance<br />

In addition to running a compiled Java program, computers running Java applications must also run the Java Virtual<br />

Machine JVM, while compiled C++ programs can be run without external applications. Early versions of Java were<br />

significantly outperformed by statically compiled languages such as C++. This is because the program statements of<br />

these two closely related languages may compile to a few machine instructions with C++, while compiling into<br />

several byte codes involving several machine instructions each when interpreted by a JVM. For example:<br />

Java/C++ statement C++ generated code (x86) Java generated byte<br />

vector[i]++; mov edx,[ebp+4h]<br />

mov eax,[ebp+1Ch]<br />

inc dword ptr [edx+eax*4]<br />

aload_1<br />

iload_2<br />

dup2<br />

iaload<br />

iconst_1<br />

While this may still be the case for embedded systems because of the requirement for a small footprint, it is<br />

frequently argued that advances in just in time (JIT) compiler technology for long-running server and desktop Java<br />

processes will close the performance gap.<br />

iadd<br />

iastore<br />

code

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!