Re: J/Link speed of accessing arrays
- To: mathgroup at smc.vnet.net
- Subject: [mg29181] Re: J/Link speed of accessing arrays
- From: tgayley at wolfram.com (Todd Gayley)
- Date: Sat, 2 Jun 2001 05:54:35 -0400 (EDT)
- Organization: Wolfram Research, Inc.
- References: <9f7k6n$gvc$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Fri, 1 Jun 2001 08:36:39 +0000 (UTC), ldwestbrook <ldwestbrook at gmx.net> wrote: >I'm trying to use Mathematica as an interactive front end to for a set >of java objects using J/Link. > >My question is this: what is the fastest way to access a given attribute >for each element in a large java array. For a 1000+ element array the >process is painfully slow using code like this on Windows NT: > >myArray = myJavaClass@getArray[]; >sum = 0; >Do[sum+=(myArray[[i]])@getAttribute[],{i,Length[myArray]}]; >Print[sum] The problem here is that calling from Mathematica into Java is comparatively expensive (perhaps a few milliseconds for an instance method like yours; much quicker for a static method). You are making one call for every element in a large array. This example is almost exactly like the one used in the J/Link User Guide for the optimization discussion in section 1.2.2.5 (Part 1/Advanced Topics/Performance Issues/An Optimization Example). You will get some speedup by using a MethodFunction for the call, but to get the best speed you will probably want to write a Java method that gets the attribute for each element and returns them all in an array. In this way, you make just one call into Java and get all the data. Of course, you could also write a Java method that computes the sum and returns that, but that would be less flexible for other uses from Mathematica. --Todd Gayley Wolfram Research