Monday, November 12, 2012

Java or .Net

In .NET you have a choice of languages to code with (C#,VB.NET, Java,Boo,Python e.t.c), producing the same type of compiled code but in Java one is limited to the java language. One may argue that jython is an alternative, but even the creator of jython who later created it's .NET version called IronPython admitted that .NET is a more powerful technology.

.NET prgrams run at native speed while java is interpreted which makes java slower.Although java has Just In Time compilation but it stills run slower. With .NET you are not limited to JIT but have the option AOT or ahead of time compilation if you want to eliminate startup delays.

Calling native code in java is not a very clean process. You need to generate some stub files which makes the whole process cumbersome and dirty. In .NET you just declare that you are calling a native function from a specified library and just start calling it.
Increased Memory Use
Java programs use about double the memory of comparable C++ programs to store the data. There are many reasons for this:
  • Programs that utilize automatic garbage collection typically use about 50% more memory that programs that do manual memory management.
  • Many of the objects that would be allocated on stack in C++ will be allocated on the heap in Java.
  • Java objects will be larger, due to all objects having a virtual table plus support for synchronization primitives. 
  • A larger memory footprint increases the probability that parts of the program will be swapped out to the disk. And swap file usage kills the speed like nothing else.

All Objects are Allocated on the Heap

Java only allocates primitive data types like int and double and object references on the stack. All objects are allocated on the heap.
For large objects which usually have identity semantics, this is not a handicap. C++ programmers will also allocate these objects on the heap. However, for small objects with value semantics, this is a major performance killer.
What small objects? For me these are iterators. I use a lot of them in my designs. Someone else may use complex numbers. A 3D programmer may use a vector or a point class. People dealing with time series data will use a time class. Anybody using these will definitely hate trading a zero-time stack allocation for a constant-time heap allocation. Put that in a loop and that becomes O (n) vs. zero. Add another loop and you get O (n^2) vs. again, zero.
The moment you start using objects in your program, Java looses the potential for optimization.
both interpreters and compilers eventually convert the source code to machine-language; after all the computer can only run a program in a machine language. A compiler does this conversion off-line and in one go; whereas the interpreter does this conversion one-program statement-by-one. A compiled program runs in a fetch-execute cycle whereas an interpreted program runs in a decode-fetch-execute cycle. The decoding is done by the interpreter, whereas the fetch and execute operations are done by the CPU. In an interpreter the bottleneck is the decoding phase, and hence an interpreted program (like Java) may be 30-100% slower than a compiled program.

Pros and cons of compiled and interpreted languages

Languages can be developed either as fully-compiled, pure-interpreted, or hybrid compiled-interpreted. As a matter of fact, most of the current programming languages have both a compiled and interpreted versions available.
Both compiled and interpreted approaches have their advantages and disadvantages. I will start with the compiled languages.
Compiled languages
  • One of the biggest advantages of Compiled languages is their execution speed. A program written in C/C++ runs 30-70 % faster then an equivalent program written in Java.
  • Compiled code also takes less memory as compared to an interpreted program.
  • On the down side - a compiler is much more difficult to write than an interpreter.
  • A compiler does not provide much help in debugging a program - how many times have you received a "Null pointer exception" in your C code and have spent hours trying to figure out where in your source code did the exception occurred.
  • The executable Compiled code is much bigger in size than an equivalent interpreted code e.g. a C/C++ .exe file is much bigger than an equivalent Java .class file
  • Compiled programs are targeted towards a particular platform and hence are platform dependent.
  • Compiled programs do not allow security to be implemented with in the code - e.g. a compiled program can access any area of the memory, and can do whatever it wants with your PC (most of the viruses are made in compiled languages).
  • Due to loose security and platform dependence - a compiled language is not particularly suited to be used to develop Internet or web-based applications.

Interpreted languages
  • Interpreted language provides excellent debugging support. A Java programmer only spends a few minutes fixing a "Null pointer exception", because Java runtime not only specifies the nature of exception but also gives the exact line number and function call sequence (the famous stack trace information) where the exception occurred. This facility is something that a compiled language can never provide.
  • Another advantage is that Interpreters are much easier to build then a compiler.
  • One of the biggest advantages of Interpreters is that they make platform-independence possible.
  • Interpreted language also allow high degree of security - something badly needed for an Internet application.
  • An intermediate language code size is much smaller than a compiled executable code.
  • Platform independence, and tight security are the two most important factors that make an interpreted language ideally suited for Internet and web-based applications.
  • Interpreted languages have some serious drawbacks. The interpreted applications take up more memory and CPU resources. This is because in order to run a program written in interpreted language; the corresponding interpreter must be run first. Interpreters are sophisticated, intelligent and resource hungry programs and they take up lot of CPU cycles and RAM.
  • Due to interpreted application's decode-fetch-execute cycle; they are much slower than compiled programs.
  • Interpreters also do lot of code-optimization, security violation checking at run-time; these extra steps take up even more resources and further slows the application down.
  • The Java Virtual Machine also takes care of security, and optimises the code. To do so, the JVM is loaded into the memory in order to run a Java program. Hence, a large JVM file will eat up system resources that might otherwise be available to the program to be executed. This may explain why Java programs feel slower.
  • Microsoft’s .NET applications cannot work without the Common Language Runtime (CLR). Most developers rely on languages such as C# or VB.NET to code using CLR. The Common Language Runtime (CLR) offers important functionalities like memory management, thread management, garbage collection and exception handling.
  • Hence, the process of compiling is faster than that of interpreting. Java architecture, however, compiles and interprets the source code only once during the process of conversion to native code.

No comments:

Post a Comment