Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI:Performance:Suggestions/Advice needed

Reply
Thread Tools

JNI:Performance:Suggestions/Advice needed

 
 
Ganesh
Guest
Posts: n/a
 
      04-03-2004
Hi
I have an issue which I am unable to decide on regarding JNI. Would be
glad if someone could help me out.
This is the requirement:

I need to pass around 20 values, all belonging to different logical
sets (for eg., 4 are related to employee details, 3 are related to
salary details etc). which finally belong to a larger logical image
(company information) from the java side to the c++ side for
processing.

I have 2 ways to do this
1. Make objects of all relevant information in the Java side, a Salary
class which will contain all salary details, an Employee class with
all employee information, and store all these in a single object
called CompanyInfo (say). I pass this object to the C++ side, which
a. uses the main object and get all the objects inside such as
salary, employee etc.
b. Calls the get methods of salary object, employee object etc. to
retrieve all the values required for processing.
C. Does processing using the required values.

2. Store _all_ the values required for the operation in a single
array/arraylist and pass it to the C++ side. C++ side retrieves it in
a pre-determined order, gets the values and does the processing.

Now, I am inclined towards 1 since the code will be well-organized,
objectified and easy to maintain. The whole design follows a logical,
constructive pattern. Whereas 2 looks crude, with everything dumped
into 1 array with no logical separation resulting in bad code which is
touhg to understand and maintain.

My problem is that I am worried about the effect on performance if I
choose 1. If 20 values are to be retrieved, then 20 callback
operations (from C++ to JNI) have to be done to call the get methods
and the values have to be got. How costly would this turn out to be?
Will option 2 be drastically cheaper in terms of time taken?

My test programs don't reveal much as there is hardly any load. This
logic will go into a web-app which will be used by thousands of users
concurrently, so could anybody please advise on which would the better
option from a performance viewpoint?

Or is there a different solution to this requirement?

If i take method 1, what about having one get method in each object
which will return an array of all the values stored in it? kinda
midway solution, I thought.

Wud be great if someone cud help me out.

Thanks and Regards
Ganesh
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      04-03-2004
On 2 Apr 2004 19:29:43 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) (Ganesh) wrote or
quoted :

>Now, I am inclined towards 1 since the code will be well-organized,
>objectified and easy to maintain.


What you want to do is keep the number of C calls to Java methods and
Java methods to C to a minimum. There is a whacking PER CALL overhead.

For max efficiency then, you pass a single blob to C that C can
process directly, rather than using accessors to call back field by
field to Java. That blob might represent a C struct, complete with
little endian data. From Java's point of view it might be a byte[]
composed with LEDataOutputStream. See
http://mindprod.com/jgloss/ledatastream.html


Before you go to such pain, make sure you truly need the speed by
doing some profiling to make sure this truly is a bottleneck.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      04-03-2004
Ganesh wrote:

> Now, I am inclined towards 1 since the code will be well-organized,
> objectified and easy to maintain. The whole design follows a logical,
> constructive pattern. Whereas 2 looks crude, with everything dumped
> into 1 array with no logical separation resulting in bad code which is
> touhg to understand and maintain.


Stick with the clean solution until you *know* you have a performance problem.
What's more, stick with it until you know that you have a problem *and* that it
can be cured (or significantly alleviated) by this sort of micro-optimisation.
Personally, I suspect that it's unlikely to help -- we are only talking about a
few micro-seconds difference (depending on the machine/JVM of course) and it
takes an *awful* lot of repetitions before that adds up to anything noticable.

If you *do* discover that you have problems, then the first step is to ensure
that you don't call the accessor methods more often that you have to -- i.e.
get the data out *once* in your C/C++ code, and keep it around for as long as
you need it.

If that doesn't work, then it's worth remembering that JNI igores Java's access
controls, so you can just read the values of the fields directly. That's
unclean, of course, but still better than mucking about with arrays (and, in
any case, I think that reading a field from JNI is about as fast as reading an
element from an array -- but don't take my word for that).

-- chris


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed on this 857W config. Repost to be clearer what the problemsare and the help needed sparticle Cisco 3 08-30-2007 07:47 PM
Ideas needed & help needed! Ryan Macy Ruby 2 07-19-2006 08:04 PM
Needed Instructor's Manual for Data Structures and Algorithms in C++ needed!!! Thomas Nick C++ 0 06-13-2005 01:58 AM
Advise needed re Olympus Camedia C-3030 Zoom Camera (driver needed) Arawak Computer Support 2 11-18-2004 03:03 PM
Microsoft small business server 2003 - help needed to understand what is needed to use it Dima Computer Support 5 10-20-2004 08:27 PM



Advertisments