Re: Java slow ?

From: Carl Howells (chowells_at_janrain.com)
Date: 05/28/04


Date: Thu, 27 May 2004 15:03:30 -0700

David Eng wrote:
>
> Java and C++ can create objects in the heap. If you compare this
> feature between Java and C++, no doubt that the speed is about the
> same.

Completely wrong. Allocating objects on the heap in Java is FAST.
(It's the initialization of the object that takes time, not the
allocation.) The new operator in C++ has to do MUCH more work to
allocate an object than the new operator in Java, because of the way
modern garbage collection algorithms lay out the heap.

> The second example is array. If Java and C++ allocate an array using
> index, the speed should be the same. However, C++ allows you using
> pointer arithmetic. Instead of locating an element of array you must
> start from the begin of the array every time, pointer arithmetic can
> locate the element based on previous position. When the elements of
> the array increase, the speed of allocating an array using index will
> much slower that pointer arithmetic.

Well, at least this isn't completely wrong. It's just mostly wrong.
It's actually fairly trivial for a compiler to optimize indexed array
lookups in a loop to pointer increments in that loop. I'm not actually
certain that this is done in any JVM, (it wasn't a few years ago), but
it certainly will be in the future.

Remember: In terms of optimization, lack of flexibility is a good
thing. When the language just doesn't let you screw up code the way C
and C++ do, the compiler can safely employ optimizations that just
aren't possible in C or C++.

Take a good compilers course sometime. You'll learn quite a bit.



Relevant Pages

  • Re: initializing an array of user-defined data types
    ... After allocating it, I want to initialize it so that after ... because your array is allocatable. ... You could even use NULLby having a component of piece be a pointer, ...
    (comp.lang.fortran)
  • Re: Limits to memory allocated in DLL
    ... >inside a DLL? ... >I am creating the following array inside a dll: ... allocating this memory in the DLL, ...
    (microsoft.public.vc.mfc)
  • Re: [ltt-dev] LTTng 0.108 provides many performance improvements
    ... Mathieu Desnoyers wrote: ... linked list to manage the buffer pages. ... the accesses done on the array will never cause vmalloc faults (I think ... vmalloc_sync_allafter allocating the array to make sure the TLBs are ...
    (Linux-Kernel)
  • Re: Ada.Command_Line and wildcards
    ... heap wouldn't work either. ... allocating objects of type array (1 .. ... I think Hyman remembers correctly: rewriting Ada programs ... was determining an array size and was of type Natural or Positive. ...
    (comp.lang.ada)
  • Re: Arrays of zero length
    ... The minor strangeness is that when you allocate an array to zero size, ... saying something like that allocating an array makes it undefined ... In f77, zero-size was disallowed, ...
    (comp.lang.fortran)