C++ vs. Java Performance

Hey guys,

My team is having difficulties choosing between C++ and java this year. Here are the considerations we need help with:

  1. Ease of use / code complexity / framework: We have a large group of qualified programmers (about 10 people). Two of us are veterans from last year who used C++ but are highly competent in Java (5’s on the AP Computer Science AB exam last year). The rest are all either proficient or approaching proficiency in Java. I’ve heard that the java code is a whole lot cleaner and easier to use than C++. Can somebody confirm this (preferably beta-testers, not random speculators. I already know the general ideas one can get from google).

  2. Code execution speed: Obviously, java will be slower than C++. Can somebody do some timing tests to find out what the slowdown factor actually is? In your experience, does the slowdown make a difference (in your present application)? Do you think it will make a big difference during build season?

  3. Ease of parallelism: Multi-threading on C++ last year was a PAIN. I know multithreading is generally easier in Java…does this rule follow on the cRIO?

We could really go either way on this decision, but I, personally, would prefer to use java if there’s no great cost to that decision.

Thanks guys!
-Matt Anderson

Adam Martin from Team 40 just gave a fantastic presentation at WPI about his experience in porting his 2009 C++ code to Java. I would defer to him on this decision, but I’ll try to summarize his points briefly:

  1. Porting the code was 90%+ copy paste find replace.
  2. Even though he had a really awesome bot, he didn’t hit performance bounds in either - he had sensor feedback on 9 of the 12 motors and several PID loops AND working vision tracking AND working vision assisted scoring in autonomous.
  3. He claims to not be a programmer.

I don’t have the link to his presentation, does anyone know where to find it?

  1. Java is definitely easier to pick up from scratch than C++, and I would argue that it is easier for C++ programmers to pick up Java than visa versa. As for the code being “clean”, that is a matter of preference, but I would tend to agree that Java looks cleaner to me (for example, all objects are passed by reference implicitly whereas in C++ you specify explicitly whether to pass by value, reference, const reference, pointer, const pointer…).

  2. Like Eric said, I don’t think any of the beta testing teams observed Java to be any slower than C++ (and the only truly taxing processing that FIRST robots do - computer vision - is outsourced to NI’s vision library which is written in native C).

  3. Again, this is a preference thing, but I prefer the simplicity of threading in Java. On the FIRST Forums, I have posted Team 341’s ported 2009 C++ code, and we use some threads in there (take a look!).

  1. Interesting…On the USFIRST.org beta testing forums, team 67 posted this:

the almost-empty loops we’ve timed run about 50 times faster in C++ than in Java.

Is there really no noticeable performance hit?

…and a couple, related question:

  1. How does the Java vision API compare to the C++ one? Is there a large speed difference?

  2. After having beta tested in java, will your team use it this year?

  3. When do you think everything will be released for us non-beta-testers?

Thanks again!
-Matt

I’m not a Beta tester, but I did talk with Brad Miller (father of WPILib) at RiverRage, and he spoke to two of your questions:

Re: Performance - Yes, C++, which compiles down to native machine code is always going to be faster than Java in pure speed tests, because Java has to take the extra step through the virtual machine. However, for our application, most (if not all) of the time-critical pieces are done in the FPGA libraries, where the JVM isn’t a factor.

Re: Vision libraries - the vision processing code remains in C++, and is accessed in Java via wrappers that use the JNI (Java Native Interface), so again, there is no Java performance penalty involved.

HTH.

I would argue that an empty loop hardly makes for a meaningful speed test. But yes - perfectly written Java will almost always lose in a performance test against perfectly written C++. But to Java’s credit, it is often easier to write good Java than it is to write good C++, which can tip the scales back in Java’s favor. Regardless, FIRST robots really don’t tend to do anything all that computationally complicated.

  1. None at all as far as I can tell. It’s the same code running underneath it all.

  2. Definitely - it is much more accessible to our students than C++.

  3. Not sure. “Soon” seems to be the general consensus. Definitely in advance of build season.

Thanks for the help, guys! At this point, it sounds like team 948 will be going java as well =D.

The Java vision stuff is actually written in C++ for high performance. In Java you refer to pointers to C++ image objects to make sure that there are no slowdowns due to any complex image processing that needs to get done.

  1. When do you think everything will be released for us non-beta-testers?

Yesterday :slight_smile:

Thanks again!
-Matt

Where can I download it?

See Bill’s Blog for the release announcement:

http://frcdirector.blogspot.com/2009/12/still-not-bill-2.html