ArrayList and native methods

Discussion in 'Java' started by Duke, Jun 27, 2003.

  1. Duke

    Duke Guest

    Hi,

    I'm writing a package in Java. In this package I have methods that take
    ArrayList objects as parameters. No problem here if all coding is done in
    Java. I may however need to later write an alternative package based on
    native methods to improve execution times. I read the Sun's Java tutorial on
    native methods but found no direct support for Collection classes. One could
    probably call ArrayList methods from the native code to access the elements
    but this is a clumsy way and I was wondering if there is a better way. Or
    should I use objects arrays instead?

    Cheers,
    Duke
     
    Duke, Jun 27, 2003
    #1
    1. Advertising

  2. On Fri, 27 Jun 2003 19:47:19 GMT, Duke wrote:
    > I'm writing a package in Java. In this package I have methods that
    > take ArrayList objects as parameters. No problem here if all coding
    > is done in Java. I may however need to later write an alternative
    > package based on native methods to improve execution times. I read
    > the Sun's Java tutorial on native methods but found no direct
    > support for Collection classes. One could probably call ArrayList
    > methods from the native code to access the elements but this is a
    > clumsy way and I was wondering if there is a better way. Or should I
    > use objects arrays instead?


    There is no need for JNI to have "direct support" for any Java
    classes. All classes are equivalent; you can create objects, invoke
    their methods and access their fields. JNI is basically just
    reflection. Sometimes it's clumsy, and usually it's a lot easier from
    Java.

    Realize that the methods in ArrayList won't be any faster if you call
    them from C than if you call them from Java. Consider that the methods
    are written in Java and calling them from C won't change that fact.

    In fact your application will probably be slower. Crossing the
    boundary between Java and C is expensive. There is additional overhead
    when you calling a method in C from Java or Java from C, so the method
    itself must be faster in order to compensate for the extra overhead of
    the call.

    Also, when you pass a Java object to a native method, the method has
    to make a series of expensive calls to JNI functions in order to
    access the object's fields and methods. You can improve the situation
    somewhat by passing only primitive data types and by doing as much
    work as possible in the native method before returning.

    Use JNI when you have problems that can't be solved in Java, or when
    you need to use existing native libraries. But don't expect that
    things will somehow automatically be faster just because you're using
    native methods.

    /gordon

    --
    [ do not send me private copies of your followups ]
    g o r d o n . b e a t o n @ e r i c s s o n . c o m
     
    Gordon Beaton, Jun 30, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,744
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,391
    Kaidi
    Jan 3, 2004
  3. xz
    Replies:
    16
    Views:
    2,382
  4. Philipp
    Replies:
    6
    Views:
    928
    Arne Vajhøj
    May 28, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    326
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page