gherardi12java
gherardi12java
Abstract. Along the years robotics software and applications have been
typically implemented in compiled languages, such as C and C++, rather
than interpreted languages, like Java. This choice has been due to their
well-known faster behaviors, which meet the high performance require-
ments of robotics. Nevertheless, several projects that implement robotics
functionality in Java can be found in literature and different experiments
conduced by computer scientists have proved that the difference between
Java and C++ is not so evident.
In this paper we report our work on quantifying the difference of per-
formance between Java and C++ and we offer a set of data in order to
better understand whether the performance of Java allows to consider
it a valid alternative for robotics applications or not. We report about
the execution time of a Java implementation of an algorithm originally
written in C++ and we compare this data with the performance of the
original version. Results show that, using the appropriate optimizations,
Java is from 1.09 to 1.51 times slower than C++ under Windows and
from 1.21 to 1.91 times under Linux.
1 Introduction
Robot software systems are concurrent, distributed, embedded, real time, and
data intensive. Computational performance is a major requirement, especially
for autonomous robots, which process large volumes of sensory information and
have to react to events occurring in the robotics operational environment.
In order to meet performance requirements, robotics algorithms have been
typically implemented in C and C++. Robotics developers in fact have always
considered C++ significantly faster than Java. Despite that, the idea of using it
in robotics is not really new: it has been followed in several projects (see section
2) and recently Willow Garage and Google have started a project for developing
a Java-based porting of ROS [4].
In this paper we report our work on the comparison of performance between
Java and C++. Our goal is to quantify this difference and to offer a set of
data in order to better understand whether the performance of Java allows to
consider it a valid alternative to C++ or not. For this purpose we implemented
in Java a well known algorithm originally written in C++ and we executed a
comparison study. The chosen algorithm is the Delaunay triangulation and its
implementation comes from the OSG library[2]. It was developed in the computer
vision field but it is typically used also in robotics for reconstructing environment
surfaces from a set of 3D points. The algorithm is well suited for the purpose of
our study because it stresses several critical points of the programming languages
performance such as: (a) the frequent access to the memory for operating on
dynamic size array (massive use of the garbage collector) and (b) the frequent
evaluation of logical conditions.
Although in the computer science domain many comparison studies has been
proposed, we considered our test interesting because we implemented and exe-
cuted the algorithm with a newer and improved version of the Java JDK. Indeed
the current Java Virtual Machine (JVM) offers a new compiler, which greatly
improves the performance of Java with respect to the older versions.
The paper is structured as follows. Section 2 reports about the Java related
projects in robotics and presents a survey on the differences of performance of
the two languages. Section 3 illustrates a performance comparison case study. We
present a Java-based implementation of a mesh generation algorithm originally
written in C++ and report several information about the execution time of both
the Java and the C++ versions. Finally section 4 draws the relevant conclusion.
During the 2011 Google I/O the researcher of Willow Garage and Google pre-
sented a new project that aims to develop a pure Java implementation of ROS
[4]. By means of this project Google and Willow Garage aim to boost the devel-
opment of advanced Android applications for robotics and easiness the access to
the cloud computing for reducing the cost of the robotics hardware.
In [11] the integration of Matlab in a distributed behavioral robotics architec-
ture is presented. The architecture is completely implemented in Java and lever-
ages on the Jini platform for distributed object registration, lookup and remote
method invocation. The Matlab integration is realized by means of JMatLink and
allows the invocation of Matlab scripts and the access to the Matlab workspace
as a distributed object. The authors present as case study a multi-robot mines
detection. In [17] a team from Lund University demonstrated that it is feasible
to develop a motion control system entirely in Java. They designed an appli-
cation that takes a picture of a person and controls a pick and place robot in
order to draw on a paper the result of the shooting. The software and the motion
controller guarantee the respect of the real time constraints by means of Java
RTS. In [16] a real-time control for a remote manipulator over a local area net-
work or over internet is presented. The developers implemented both the control
system and the teleoperation of the robot in Java. In [9] an autonomous motion
planning system completely developed in Java is developed. The application al-
lows the user to set up the working environment though a graphical interface
and offers the functionalities of collision detection, obstacle avoidance, free-paths
generation and selection of the shortest path. Finally in [14] an application for
controlling robots through the World Wide Web is implemented. The software
is designed for dealing with low bandwidth and high latency and allows the
operator to control the robot from any computer connected to the web.
and standard C. This benchmark was introduced by Jack Dongara and measures
how fast a computer solves a dense N-by-N system of linear equations. The
results showed that for a 1000 x 1000 system Java was 2.25 times slower than
C. In [18] Ruolo evaluated the Java method call performance. Different tests
with a different numbers of parameters showed that Java was only one clock
cycle slower than C++. The same tests also highlighted that the time needed
for allocating user defined objects on the heap was roughly equivalent. However
C++ also uses the stack for allocating temporary object and in this case it
was from 10 to 12 times faster than Java, which uses only the heap. Interesting
conclusions were reported by Mangione [12]. He tested the repetitive execution
of simple operations like integers and float divisions and showed that Java was
as fast as C++. As summarized in table 1 all the papers report that, since the
introduction of the Just-In-Time compiler, Java is only 1.45-2.91 times slower
than C++ (Column OS: operating system, columns 4-5 execution time ratios).
Since these studies demonstrated how the execution of simple operations in
Java is more or less as fast as in C++, one factor that could influence the total
execution time of a Java program is the Garbage Collector (GC). However [10]
showed that Java GC is as fast as a malloc/free operation in C++. In fact when
a program executes a malloc operation, the allocator looks for an empty slot of
the right size and returns a pointer to a random place in the memory. In Java
instead the allocator use the bits of memory adjacent to the last bit it used.
Hence it doesn’t need to spend time looking for memory. So the amount of time
used for the garbage collector is comparable to the amount of time that the
allocator uses in C++ for finding free memory slots.
Finally other interesting results are documented in [15]. The same program
was implemented by 40 different programmers in different languages (24 in Java,
11 in C++ and 5 in C). The experiment compared not only the performance of
the languages but also the differences between the implementations in the same
language (interpersonal differences). The results demonstrated that Java was 2
times slower than C++ and that the interpersonal differences were much larger
than the average difference between Java and C++. That means a well written
Java program could be as efficient as an average C++ program.
We have also analyzed time required for the evaluation of logical conditions.
Four tests were executed, taking into account the following logical conditions:
– Simple logical proposition (var==true)
– Disequation (a < b). (Most evaluated condition in the case study, see eq. 1)
– Logical disjunction of two disequations ((a < b)||(a > c))
– Logical conjunction of two disequations ((a > b)&&(a < c))
Each evaluation was executed 10000 times and each test was repeated 50 times.
Table 3 reports average times of the tests in milliseconds. We used a boolean
variable (initialized false and its value was changed each execution (var = !var ))
in the first test and float variables (initialized with a constant values) in the
others. As can be seen, Java is always faster than C++, except for what regards
the evaluation of simple logical proposition.
points are inserted in order to surround the plain point cloud. These four
points are used to build two main triangles (super-triangles), such that the
plain point cloud lies inside their area. These triangles are stored in a collec-
tion, that we’ll call trianglesList. The collection data structure was chosen
accordingly to the operations that occur more often, indeed the trianglesList
is subject to several iterations, insertions and removal. As shown in [20], Ar-
rayList is the list of all the available lists in the Java framework that perform
insertion, iteration and random access in the fastest way. Although removing
objects from ArrayList requires a long time, insertions and iterations occur
more often than remove operations; hence we decided to use ArrayList for
implementing the trianglesList.
2. Iteration. During the iteration, each point is considered and is compared to
the triangles contained in the trianglesList. First the condition 1 is checked
(“point” stays for the current point and “tri.circ for the circumcircle of the
current triangle).
– If it is true, the current triangle is removed from the triangleList and will
not be more considered because the current point and also the following
ones surely don’t lie in the circumcircle of the current triangle (i.e. the
triangle respects the Delaunay condition for all the points and it is part
of the final mesh). This is guaranteed by the initial ordering of the points.
– Otherwise, we must further investigate if the current point effectively
lies in the circumcircle of the current triangle. In case it is true the
Delaunay condition is not respected. Therefore the edges of the triangle
are added to a specific ArrayList (called edgeSet) and the current triangle
is deleted. Whereas, if the Delaunay condition is respected, the next
triangle is considered. It has to be noted that the edgeSet collection
has been implemented as an ArrayList because it is sorted many times
during the triangulation algorithm. Hence, the usage of Collections.sort
method and ArrayList is the Java solution that allows us to save time
and increase performance in the best way. In our tests the maximum size
of the edgeSet collection was never greater than 100.
When the whole trianglesList has been scanned, new triangles are con-
structed from the edgeSet collection and added to the triangleList (in our
tests the maximum order of magnitude of this collection size is 10000). Note
that if an edge is shared between two triangles that contain a point, then
the edge is not considered. The iteration proceeds until all points have been
analyzed, except for the four points created during the initialization .
3. Completion. The four points introduced during the initialization step and tri-
angles having vertices in common with these four points are deleted. If there
are degenerate triangles (circumcircle radius equals to 0) they are eliminated
too. Finally a return result is built in form of a Mesh.
Since the order of magnitude of the size of the list on which we perform more
insertion is 10000 whereas the one of the collection on which we perform the
sorting is 100 the time gained in Java for populating the first collection is lost
for sorting the second one (see table 2). This suggests that the time spent for
managing collections will be more or less the same for both the Java and C++
implementations. The evaluation of logical conditions instead seems to be not
important from a performance point of view. In fact the time spent for evaluating
conditions on 10000 iterations is much lower than the time spent for populating
collections in more or less 100 iterations.
Referring to the table 4, the triangulation execution time obtained with the
first point cloud (sphere) is not very truthful. Indeed Java version is 9.42 times
slower than C++ and this value doesn’t fit the ratios obtained with the other
point clouds. In this case the execution time is very small and so the time
required for compiling the code greatly influences the result. Note that the costs
required by the compiler have a fixed part, which is the same for each point
clouds. Hence the smaller is the point cloud, the greater is the influence of the
compilation overhead on the execution time.
discussed in section 2. Indeed in our tests the execution time ratio between Java
and C++ goes from 2.37 to 5.61 against the range 1.45-2.91 reported in table 1.
One possible reason is that the triangulation process requires an intensive use
of the memory and probably the C++ version leverages the possibility of store
temporary objects on the stacks, which is much faster [18].
The table shows that under Ubuntu Java is more efficient than under Win-
dows. Indeed the ratio range goes from 2.06 to 4.04. It should be noted that we
executed the tests with both the OpenJDK and the Sun JDK. However in the
paper we consider only the first one because the results are almost the same.
Another consideration can be done on the relation between the point clouds
sizes and the execution time ratios. The value of the performance ratio doesn’t
show a linear trending, hence we can assert that for this algorithm there is no
correlation between the performance ratio and the input size.
5 ACKNOWLEDGMENTS
The research leading to these results has received funding from the European
Community’s Seventh Framework Programme (FP7/2007-2013) under grant agree-
ment no. FP7-ICT-231940-BRICS (Best Practice in Robotics). The authors
would like to thank all the partners of the project for their valuable comments.
References
1. Just in time compiler. http://en.wikipedia.org/wiki/JIT_compiler.
2. Open Scene Graph. http://www.openscenegraph.org.
3. Open Scene Graph API reference. http://www.openscenegraph.org/
documentation/OpenSceneGraphReferenceDocs.
4. Rosjava - An implementation of ROS in pure Java with Android support. http:
//code.google.com/p/rosjava/.
5. Specifications of java.util.collections. http://docs.oracle.com/javase/6/docs/
api/java/util/Collections.html.
6. L. Bernardin, B. Char, and E. Kaltofen. Symbolic computation in Java: an ap-
praisement. In Proceedings of the 1999 Int. symposium on Symbolic and algebraic
computation, pages 237–244. ACM, 1999.
7. J. Bull, L. Smith, M. Westhead, D. Henty, and R. Davey. A methodology for bench-
marking Java Grande applications. In Proceedings of the ACM 1999 conference on
Java Grande, pages 81–88. ACM, 1999.
8. B. Delaunay. Sur la sphere vide. Izv. Akad. Nauk SSSR, Otdelenie Matematicheskii
i Estestvennyka Nauk, 7:793–800, 1934.
9. A. Elnagar and L. Lulu. A global path planning Java-based system for autonomous
mobile robots. Science of Computer Programming, 53(1):107–122, 2004.
10. J. Lewis and U. Neumann. Performance of Java versus C++. Computer Graphics
and Immersive Technology Lab, University of Southern California, Jan, 2003.
11. M. Long, A. Gage, R. Murphy, and K. Valavanis. Application of the distributed
field robot architecture to a simulated demining task. In Robotics and Automation,
2005. ICRA 2005. Proceedings of the 2005 IEEE Int. Conference on. IEEE, 2005.
12. C. Mangione. Performance tests show java as fast as c++. JavaWorld, 1998.
13. S. Meloan. The Java HotSpot (tm) Perfomance Engine: An In-Depth Look. Article
on Suns Java Developer Connection site, 1999.
14. F. Monteiro, P. Rocha, P. Menezes, A. Silva, and J. Dias. Teleoperating a mo-
bile robot. A solution based on JAVA language. In Industrial Electronics, 1997.
ISIE’97., Proceedings of the IEEE Int. Symposium on, volume 1. IEEE, 2002.
15. L. Prechelt et al. Comparing Java vs. C/C++ efficiency differences to interpersonal
differences. Communications of the ACM, 42(10):109–112, 1999.
16. F. Raimondi, L. Ciancimino, and M. Melluso. Real-time remote control of a robot
manipulator using java and client-server architecture. In Proceedings of the 7th
Int. Conference on Automatic Control, Modeling and Simulation, 2005.
17. S. Robertz, R. Henriksson, K. Nilsson, A. Blomdell, and I. Tarasov. Using real-
time Java for industrial robot control. In Proceedings of the 5th Int. workshop on
Java technologies for real-time and embedded systems, pages 104–110. ACM, 2007.
18. M. Roulo. Accelerate your Java apps. Java World, 1998.
19. S. Spw, S. Wentworth, and D. Langan. Performance evaluation: Java vs c++. In
39th Annual ACM Southeast Regional Conference. Citeseer, March 16-17 2001.
20. S. Wilson and J. Kesselman. JavaTM Platform Performance - Chapter 8.
Sun Microsystems, 2001. http://java.sun.com/docs/books/performance/1st_
edition/html/JPAlgorithms.fm.html.