ArrayList and native methods

D

Duke

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
 
G

Gordon Beaton

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top