Results 1 to 6 of 6
  1. #1
    Steven Miller is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Cool Question on the speed of c code when called from Java

    :oI am a graduate student working on a research project. I am writing my interface in Java using JEE environment (so Java servlets with Apache). This is not the point of the question though so I didn't stick this in a different thread. The point of the question is that I need to call some native code from Java (c, c++) which is very computationally expensive and includes advanced numerical approximations along with advanced math concepts, and I need it to be fast! I don't want to rewrite the code in Java for a few reasons. Some of these reasons are that Java can not compute them as fast as something such as c and I don't have all year to rewrite everything. I came across a java tool called the Java native Interface that allows you to do this. The question I have is speed.
    -When c is called by Java in this way does it invoke say a gcc compiler and run the c code in its native environment or does it run it through the Java Virtual Machine and other APIs?

    -If the c code has to run through the Java compiler then is there a way to just call a compiled c program from Java and not compute anything from the Java compiler?

    -If I don't get any speed benefits in calling c from Java in the above manner, is there a way that I can?

    -Any suggestions on getting the speed benefits of c out of Java differently?

    If I am asking the wrong questions then please let me know. The piont is I want the interface to be in Java and I want the complex and high computationally intensive tasks to be in c. There are several reasons why I want to use c for this part, but the biggest is speed differences compared to writing something like this in Java.

    Any help would be appreciated.
    Thanks, Steven Miller :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Steven Miller View Post
    I don't want to rewrite the code in Java for a few reasons. Some of these reasons are that Java can not compute them as fast as something such as c
    Not necessarily true as just in time compiler and JVM are pretty smart, but that's not the thrust of this thread, so I'll leave it alone.

    -When c is called by Java in this way does it invoke say a gcc compiler and run the c code in its native environment or does it run it through the Java Virtual Machine and other APIs?
    Your C/C++ code had better already have been compiled before you try to run it from Java. For example in Windows you can't call a C or CPP file from the JNI but rather a dll, and this makes sense. So it should run as fast as the dll normally would run its code.


    -If the c code has to run through the Java compiler then is there a way to just call a compiled c program from Java and not compute anything from the Java compiler?
    The Java code had better also have already been compiled, so the Java code will be run by the JVM.

    -If I don't get any speed benefits in calling c from Java in the above manner, is there a way that I can?
    Why not test it and see what happens.

    -Any suggestions on getting the speed benefits of c out of Java differently?
    JNA is usually easier to use than JNI, but the JNA folks say straight out that it's a little slower than JNI (which it uses internally).


    If I am asking the wrong questions then please let me know. The piont is I want the interface to be in Java and I want the complex and high computationally intensive tasks to be in c. There are several reasons why I want to use c for this part, but the biggest is speed differences compared to writing something like this in Java.

    Any help would be appreciated.
    Thanks, Steven Miller :)
    Are you a graduate student in computer sciences? If so, you will want to review what a compiler is and what it does.

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    When you use JNI, you build your code using, say gcc. You build it as a shared object, when then gets loaded by the JVM at runtime in the same way shared objects are typically loaded. The code will run at the exact same speed as it would if you loaded the shared object from c instead of from java. In other words, it will do exactly what you need it to do.

    That said, passing data back and forth between Java and native code can result in a performance bottleneck if you're passing tons of data around. This usually isn't an issue, but it's something to keep in mind.

  4. #4
    Steven Miller is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default

    I appreciate both comments and for the speedy replies. I think I should make a few clarifications because some of my points are a little misunderstood (my fault). I appreciate the points Fubarable made about considering what is going on in the compiler because this clarifies and rounds out things. What I intentionally meant for Java being slower though is the complexity and overhead in the language itself such as running objects from the compiled code and other things built into the language vs not having that in c (also memory management issues and the disadvantage of not being able to use pointers directly in Java). Theoretically then ya, The Java Compiler does not make things necessarily slower, but the overhead in Java when the program needs to run (from the more complex compiled code) makes things slower. (I haven't ever tested gcc compiler against Java VM compiler for code to code differences though) As a general rule Java runs slower than c in my experience (From tests I've done in advanced algorithms on problems in the past then it seems to differ from c alot slower) I didn't want my c code to get wrapped up in the logistics of the Java language.

    I also appreciate toadaly's comments, this is more of the answer I was looking for.
    So just to verify and make sure I understand: the c code should already be compiled in something of my choice (say gcc) and stored in a library/dll shared object thing (I will need to research up on this in JNI and how it does it). As the Java code is compiled it pulls in the already compiled c code(which is what we are calling a shared object) and makes it apart of the compiled Java code. Thus, we don't lose any speed at run time because it is one big compiled file with the Java code compiled in Java Compiler and C code compiled in C compiler and the c code is not compiled in a bunch of the Java language overhead as far as the c file is concerned.

    (Or does the compiled Java code have to call a seperate compiled c file in the library when it hits the c portions and keep these files seperated from each other, thus the reason for having to watch for slowness in calling shared objects in this form?)

    Basically I will be using Java for the GUI and C for computational intense stuff like multi-dimension integration over matrices in Euclidean space and imaginary domains.

    Please reply back I want to make sure I understand this correctly, this could make a big difference for us for the next 3 months. :)

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    With JNI, you will be compiling your c code using your compiler of choice. The *only* difference, is you will compile it into a dynamic library (a .dll in windows nomenclature, or a .so in unix nomenclature) instead of an executible. This is not hard. It's just a command line option on your compiler.

    Keep in mind that 'java' is an actual binary application just like any other. It can load native libraries dynamically just like any other application can. ...and that's what it does when you use JNI. When an application loads a dynamic library, there is no compilgin going on at that point. It's just executing precompiled code.

    Your overall plan is good, but there is an alternative if you feel uncomfortable with JNI (which you should because in addition to learning java, you also have to learn the JNI API). If the code you're planning to call has a fairly straightforward interface, say you feed it a block of data and it crunches for a while and then returns a block of data, then you could use Runtime.exec() instead of JNI, and feed files to your application, getting files back in response. Since all modern OS's cache, the files will never actually touch the disk as long as they are not too big and as long as you dutifully delete them as soon as you're done processing. You can often achieve the same performance with this approach as with JNI. But there are gotchas with Runtime.exec() that newbies usually overlook, so if you want to go that approach, make sure you read up on the right way to use it or you'll end up running out of file descriptors.

  6. #6
    Steven Miller is offline Member
    Join Date
    Feb 2011
    Posts
    7
    Rep Power
    0

    Default Problem Solved

    Thanks you guys, this has been most helpful!:)

Similar Threads

  1. How do I create a class called Dog in Java?
    By rahilz in forum New To Java
    Replies: 19
    Last Post: 12-19-2010, 11:08 PM
  2. Can C++ routines be called from Java program
    By gkk in forum New To Java
    Replies: 2
    Last Post: 07-20-2010, 10:43 AM
  3. Replies: 4
    Last Post: 07-17-2010, 11:50 AM
  4. question about so-called "memory consistency errors"
    By gib65 in forum Threads and Synchronization
    Replies: 2
    Last Post: 06-20-2010, 05:20 PM
  5. need a java code for this question!
    By rose in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:34 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •