 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Duke Guest
|
Posted: Fri Jun 27, 2003 7:47 pm Post subject: 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
|
|
| Back to top |
|
 |
Gordon Beaton Guest
|
Posted: Mon Jun 30, 2003 7:51 am Post subject: Re: ArrayList and native methods |
|
|
On Fri, 27 Jun 2003 19:47:19 GMT, Duke wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|