MathGroup Archive 2001

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

Search the Archive

Re: C, MathLink or Java, J/Link

  • To: mathgroup at smc.vnet.net
  • Subject: [mg28145] Re: C, MathLink or Java, J/Link
  • From: tgayley at wolfram.com (Todd Gayley)
  • Date: Sun, 1 Apr 2001 00:08:05 -0500 (EST)
  • Organization: Wolfram Research, Inc.
  • References: <99usl9$61j@smc.vnet.net> <9a1jfh$9o8@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 30 Mar 2001 04:26:09 -0500, Jens-Peer Kuska <kuska at informatik.uni-leipzig.de> wrote:

>Hi,
>
>J/Link is great for the creation of new user interaces. But
>in general Java is too slow, especial if you are looking for
>speed.
>
>Here at the computer graphics department we had serval
>web-based visualisation applications (2d, 3d) and 
>for these applications Java was selected as an "ideal"
>language ...
>
>*All* these applications switch back to C/C++, only
>due incredible slow speed.
>
>An applications, that are more complex than to play 
>Tick Tack Tao are too slow.
>
>If you like the feeling of a 12 MHz 8086 computer you
>must use Java. It bring's you back in time by 10 years.
>
>Since Java *and* Mathematica are well known as lighting fast, 
>it is a wonderful idea to couple these two racing horses ;-)
>
>Regards
>  Jens


Jens,

I have to vigorously dispute the claim that Java is slow. Many times when you hear this
claim, it is based on experience with old versions of Java (like 1.0.2 or even earlier).
Java has improved enormously over the years. The other day I was looking at some
benchmarks (I don't remember where) that compared one numerical test across a large number
of OS's and Java virtual machines. The lowest number was 12, achieved on a 200 MHz PC with
an early Java version. The highest number was 7700 or so, achieved on a 700 MHz PC with a
modern Java version. That's a factor of 640 for machines that differ in raw speed by at
most a factor of 4.

It is also the case that GUIs built with Java tend to be noticeably slower than those
built with C/C++. Since GUI speed is a very visible manifestation of performance, this has
contributed to an erroneous perception of Java's overall speed.

Without a doubt, there are some operations for which Java is known to be relatively slow,
and there are classes of programs for which C trounces Java. For the most part, these are
the exception rather than the rule. You mentioned 3D graphics--Java now has a 3D API with
implementations that talk directly to graphics accelerator hardware.

Here I present some timings I measured for an identical sieve of Eratosthenes benchmark in
C and Java. This program is a relatively tight loop, and thus is admittedly a best-case
scenario for the just-in-time compilers and other optimization technologies that Java
virtual machines use.

Windows NT, 500 MHz Pentium III.

 Timing (secs.; lower is better)
------------
      9.7     Java 1.1.7
     10.5     C with compiler optimizations on
     12       Java 1.2.2
     21       Microsoft Java
     22       Java 1.3 Hotspot Server VM
     26       Java 1.3 Hotspot Client VM
     36       C with compiler optimizations off
    283       Java 1.1.7 WITHOUT just-in-time compiler

You can see that Java 1.1.7 is _faster_ than optimized C code on this benchmark, and Java
1.2.2 is nearly as fast. Take a look at the last line, the timing for Java without a
JITC--30 times slower than with a JITC. If you had only experienced Java in the Bad Old
Days before JITCs you would have a very incorrect perception about performance. Don't make
too much out of the curious trend in these numbers that newer Java versions are slower
than older ones. Overall performance has improved with each release. (This serves to
demonstrate that one isolated benchmark is not terribly revealing.)

Are these results typical of numerical programs of the sort Mathematica programmers are
likely to be writing? I would say yes. I usually expect a Java program to run in a range
comparable to C down to perhaps 1/2 to 1/3 as fast.

As always, "Your mileage may vary". Nevertheless, people should not rule out Java in
advance as a language for virtually any kind of program based on the (mostly erroneous)
assumption that it is "too slow". 

As for the original question, about whether to use Java or C for an algorithm to be called
from Mathematica, the answer is of course "it depends". Some factors that favor C are:

- you already are proficient in C
- you just have one function to call and you don't need to have a
  complicated interaction with Mathematica
- you don't need to port your program to other platforms
- flat-out performance is overwhelmingly important

One thing I would NOT recommend is to decide to become proficient in C and MathLink just
so that you can write algorithms to plug into Mathematica. Someone in that situation
should instead become proficient in Java. It's a more valuable language right now, and
most programmers won't have to learn anything about MathLink--you will just write Java
programs that do something, and call them directly from Mathematica. In other words, when
you use C for the job, you have to write "a MathLink program." When you use Java, you
usually just write an ordinary Java program, with no special consideration that it will be
used from Mathematica.


--Todd Gayley
Wolfram Research


>> I am working on an algorithm and am puzzled about which way to proceed.  I
>> always knew my algorithm would run much faster if I wrote it in C and used
>> Math Link.  I recently decided to become proficient in C and MathLink so I
>> could do just that.  But then I read in Todd Gayley's J/Link User Guide that
>> 
>> -- Java is the ideal language for writing programs that use the
>> computational services of Mathematica.
>> -- You can use Mathematica to in effect, write a Java program in
>> Mathematica.
>> -- Essentially anything you can do from Java you can now do from
>> Mathematica, perhaps even more easily.
>> -- Java is fast, robust, portable, and an excellent general-purpose
>> programming language.
>> -- Java with J/Link is easier than using the traditional MathLink interface
>> from C or C++.
>> -- The syntax and intricacies of the Java language are not relevant, since
>> you will be writing Mathematica programs, not Java ones.
>> 
>> After reading all these great things about Java and J/Link I am wondering if
>> I should write my program using Java and J/Link. Can anyone provide advice?
>>
>



  • Prev by Date: Novice question regarding variable naming and subscripts
  • Next by Date: RE: Novice question regarding variable naming and subscripts
  • Previous by thread: Re: Re: C, MathLink or Java, J/Link
  • Next by thread: ToExcel