Elegance of Java and the Efficiency of C—It's Ada!

December 1st, 2000 by Frode Tennebo in

Tennebo recommends taking a look at Ada.

In his article “Comparing Java Implementations for Linux”, Michael Hirsch (LJ August 2000) searches for “a programming language with the elegance of Java and the efficiency of C”. This language already exists and it's called Ada.

The current version of Ada, Ada 95, is a revision of the Ada standard (Ada 83 was the original ANSI/ISO standard). It was standardized while C++ and Java were still in early childhood. Ada is often considered a bulky, antiquated language without the coolness of Java or the seriousness of C++. However, Ada is a powerful and modern language.

Ada has all the features of Java in addition to some of its own (such as generics and operator overloading. Actually, Ada provides all the features of Java and C++ (except for direct support for multiple inheritance) in one language, their web site (see Resources) provides a good comparison of the languages. You can even build j-code applets from Ada source. Thus, you have all the advantages of Java and more.

Ada is a strongly typed language which, if used correctly, can lead to less ambiguous code and more maintainable programs. The result is that errors can be detected easily at compile time instead of at debug time. More information on the Ada language can be found at their web site.

There are a number of Ada compiler vendors around, and you can get one for virtually any platform which, again, can cross-compile to (almost) any other platform. The most well known vendors are Rational, Aonix, R. R. Software and Green Hill Software (see Resources).

However, Michael Hirsch also wanted an open-source project. In reply, GNAT from Ada Core Technologies is to Ada as gcc is to C/C++, almost. GNAT is written almost entirely in Ada and uses the gcc code generator. The public version of GNAT (see Resources for site where you can download it) is distributed under the FSF copyleft license. ACT also provides a nonpublic version of their compiler under a support contract.

I have written the equivalent Ada program to Michael Hirsch's Java and C++ programs as shown in Listing 1. As you can see, the listing is highly readable and looks very much like both the Java and C++ program. It is divided into three units, a package specification (like the C header files), a package body and the main program (a procedure). Ada has specific rules concerning what goes in which unit, and GNAT enforces this by requiring each unit to be in a separate file, where the file name must be the package/procedure name postfixed with .ads for specification and .adb for package/procedure.

Listing 1. Ada Equivalent of Java and C++ Programs

You can type in the example (or download it) into one file and run the utility gnatchop, which is included in the GNAT distribution. This will split the file into the three required files with the respective filenames. To compile and link, you can use the specially modified GNAT version of gcc, but it's more practical to use the gnatmake utility. Just type: gnatmake perftest and gnat will compile the required units and then bind and link the main procedure.

Since I do not have the same hardware as the author, I have performed the same Java and C++ benchmarks, in addition to the Ada benchmark, on two Linux machines and a ten-inch Sun Ultra. The results are shown in Table 1; the elapsed time is objects per millisecond.

The Ada compilers used were Gnat 3.12p, Gnat 3.13p and JGNAT 1.0p (p = public). The latter two were just released at the time of this writing . Please note that there are other Ada compilers that produce executables that may run faster or slower than the GNAT compiler.

Table 1. Results of Ada, Java and C++ Benchmarks

JGNAT is a peculiar beast, as it compiles Ada code into Java byte-code which can, in turn, run on any JVM. As you can see, it is only about 15% slower than the SUN JDK and JRE 1.2.01 is actually faster! And that is for precisely the same code compiled with the GNAT compiler, using the very first version of the JGNAT compiler! This can only get better.

I tried running the JGNAT-compiled programs using kaffe on Linux, but got:

[ft@modesty jtest]$ ~/kaffe/bin/java perftest@bbKaffe: mem/gc-incremental.c:823: gcMalloc: Assertion <\#145>fidx < nrTypes
&&
size != 0' failed.

This suggests a bug in kaffe.

A small note on Ada in general and GNAT in particular: By default, the compiler compiles in many of the runtime tests to certify that everything goes smoothly. Therefore, if there's a problem, it can make a controlled action to avoid premature program termination. Usually, the action is to raise an exception. The -gnatp disables many of these tests and, hence, makes the program run faster. I do not recommend doing this, but it does provide for a better comparison with the C++ speeds.

For this particular test, Ada is about 19% slower on average than C++. On the Sun platform, this is down to about 8% using a comparable code generator (GNAT uses gcc 2.8.1 as its base). Note that this is a very limited test, and it only goes to show that Ada can produce running speeds comparable to C++. In some cases, Ada will actually run faster than the C++ counterpart. My guess is that the difference will level out, perhaps slightly in C++'s favour. The usual disclaimer applies.

However, it is in development and, later, in maintenance that the real savings can be harvested with Ada. Refer to www.rational.com/products/whitepapers/337.jsp for a thorough comparison between Ada 83 and C.

Conclusion

If you want a programming language with the elegance of Java (and more) and the speed of C/C++--take a look at Ada. It will even produce Java byte-code.

Resources

Frode Tennebø (frodet@nvg.org) lives in Halden, Norway. He first encountered Linux in 1994 and has been a Linux enthusiast ever since. He holds a seat on a local LUG board. Currently he spends most of his spare time with his five-month-old baby girl, leaving less time for computers.

__________________________


Special Magazine Offer -- 2 Free Trial Issues!
Receive 2 free trial issues of Linux Journal as well as instant online access to current and past issues. There's NO RISK and NO OBLIGATION to buy. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Sorry, offer available in the US only. International orders, click here.

Featured Videos

Non-linear video editing tools are great, but they're not always the best tool for the job. This is where a powerful tool like ffmpeg becomes useful. This tutorial by Elliot Isaacson covers the basics of transcoding video, as well as more advanced tricks like creating animations, screen captures, and slow motion effects.

Shawn Powers reviews the HP Mini-Note portable computer.

Thanks to our sponsor: Silicon Mechanics

Silicon Mechanics is a leading manufacturer of rackmount servers, storage, and high performance computing hardware. The best warranty offerings available are backed by experts dedicated to customer satisfaction.

From the Magazine

August 2008, #172

There's nuttin like a Cool Project to give you some relief from the summer heat, so get out your parka cuz we got a bunch of em. First up is the BUG, not a bug, The BUG. It's got a GPS, camera and more, in a hand-sized package that's user programmable. The BUG does everything. It's both a floor wax and a dessert topping. Get one now. Need a software version of a Swiss Army knife? Take a look at Billix, and don't leave home without it. Then, chew on this one, an X server on a Gumstix device driving an E-Ink display. Need more storage? How about 16 Terabytes? Can do.

And, of course, we have the usual cast of characters: Marcel, Reuven, Dave, Kyle, Doc, plus the new kid on the block Shawn Powers. But it doesn't stop there: build a MythTV box on a budget, build your own GIS system, set up the tools to monitor your enterprise and more. Finally, remember The War of the Worlds? Now you can play too.

Read this issue