MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: J/Link speed of accessing arrays

  • To: mathgroup at
  • Subject: [mg29181] Re: J/Link speed of accessing arrays
  • From: tgayley at (Todd Gayley)
  • Date: Sat, 2 Jun 2001 05:54:35 -0400 (EDT)
  • Organization: Wolfram Research, Inc.
  • References: <9f7k6n$gvc$>
  • Sender: owner-wri-mathgroup at

On Fri, 1 Jun 2001 08:36:39 +0000 (UTC), ldwestbrook <ldwestbrook at> 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;

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 (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

  • Prev by Date: Re: Differential eqn. with interpolated coeficients
  • Next by Date: Re: Compound Expressions
  • Previous by thread: J/Link speed of accessing arrays
  • Next by thread: freebsd...