Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   ArrayList and native methods (http://www.velocityreviews.com/forums/t123779-arraylist-and-native-methods.html)

Duke 06-27-2003 07:47 PM

ArrayList and native methods
 
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



Gordon Beaton 06-30-2003 07:51 AM

Re: ArrayList and native methods
 
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


All times are GMT. The time now is 08:33 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.