6 ms·
FTA: > Though you cannot reach the same kind of speed in Java as you can in C++, there are many good reasons to use Java instead of C++. How good is Java at th
by beagle3 13y ago
FTA:
> Though you cannot reach the same kind of speed in Java as you can in C++, there are many good reasons to use Java instead of C++. How good is Java at this task? Direct comparisons between Java and C++ are difficult. I would estimate that the difference is a factor of 3 and more. But Java can still be more than fast enough.
That has also been my experience, but there's always an army of people that claims "Java can be just as fast or faster than C++". Anyone care to prove Mr.Lemire wrong? (My experience was more like twice as slow for memory intensive code, but thrice as slow still sounds like a reasonable estimate to me)
- noelwelsh 13y agoThe answer is that if you're prepared to off piste, mainly by using sun.misc.Unsafe, you can get within a gnat's whisker of C++. For example: http://mechanical-sympathy.blogspot.co.uk/2012/07/native-cc-like-performance-for-java.html http://mechanical-sympathy.blogspot.co.uk/2012/07/native-cc-... and http://mechanical-sympathy.blogspot.co.uk/2012/10/compact-off-heap-structurestuples-in.html http://mechanical-sympathy.blogspot.co.uk/2012/10/compact-of... It's interesting to look at what Java lacks to achieve this level of performance with less pain. The main issue, I believe, is control over memory layout. For example, avoiding boxing in arrays of objects. Avoiding GC is also an issue. I think Rust is interesting as it allows the programmer to talk about these things without going "outside" the language like one must in Java, while still retaining a modern programming style.
- beagle3 13y agoThat's a good answer, but it isn't really Java - it's assembly in disguise. You lose basically everything Java can give you when you do that. And I don't know since when it actually works well - as of 2011 with Java 1.6, I had similar code that used memory mapped buffers, and crawled like a snail because (apparently, I couldn't directly verify) the optimizer would not inline the memory mapped access, meaning that every array access cost ~10 times as much as it should.
- noelwelsh 13y agoI agree it isn't great, but it works, and if you need that extra performance in a small part of your application it can allow you to stay on the JVM. I'd rather do that than write everything in C (YMMV.) The blog posts I linked compare performance to nio ByteBuffers, and show improvement for Unsafe over ByteBuffer. That might be relevant to your case.
- beagle3 13y agoI rewrote in C, and got 10x speedup and x4 less memory. So, it's no longer relevant :)
- kasey_junk 13y agoYou can say that about every technique that goes 1 level lower in abstraction. Personally, these techniques allow me to write the vast majority of my code in a high level JVM language (Scala in my case) and optimize the very small percentage of my code that needs to be this latency sensitive. Because I can get so close to native performance in that highly specific case, the idea of using C++ hardly ever comes up. If I need faster than this, C++ is probably not fast enough either and I need to look at "closer to iron" solutions. I for one appreciate that. But then again, I'd much rather program in C than C++. As far as claims like Java is 3x slower than C++, it is nearly impossible to quantify these things directly. For any given benchmark you will be able to find people who complain about the code in the benchmark not being optimized enough. Suffice it to say that there are some things Java is faster at and some C++ is and their are techniques in both that allow them to achieve near parity.
- anonymous 13y agoIf you're prepared to do all that, why not write the hot code in C and use JNI?
- noelwelsh 13y agoIf you go C/JNI you: 1. Have added an extra step to your build process 2. Pay the cost of doing JNI calls, which I believe is quite high. Look, I'm not saying doing this Unsafe stuff is rainbows and unicorns. It's a hack, but it's a hack that is extremely useful in a few niche applications.
- kasey_junk 13y agoJNI calls carry a very significant latency overhead. If the code you are trying to optimize would have to call the JNI methods repeatedly you very quickly lose any speed advantage you had from writing it in C. If on the other hand you only need to call the JNI a few times it probably does make sense to go the JNI route. Another common option (especially in server style programs) is to have 2 daemons, one in C (or C++) doing a very specialized role and communicating with a JVM daemon via memory mapped files.
- qwerta 13y agoHave look at my project: MapDB. It provides java collections on top of raw byte buffers or files.
- noelwelsh 13y agoNice work! Never heard of this before. Looks like it might be a useful replacement for Redis in some cases.
- kryten 13y agoI can afford twice as powerful kit when I write in Java as it takes less time :)
- beagle3 13y agoTry Python or K, you'll be able to afford 5 to 100 times as much :)
- brazzy 13y ago> That has also been my experience, but there's always an army of people that claims "Java can be just as fast or faster than C++". Anyone care to prove Mr.Lemire wrong? They are not necessarily in contradiction. Java can be as fast or faster than C++ - if you choose a workload whose active set (including Java overheads) fits into the CPU cache. Of course there are very few realistic and useful workloads where that is the case. Still, most things don't have code execution as a bottleneck and/or aren't so performance critical that a factor of 3 is a big problem.
- pron 13y agoI'd like to say something that may sound a bit controversial, but bear with me: for most practical programs, Java's performance beats C++; in fact, it beats C. And assembly. There, I've said it, and now let me try to back this up. Proving my claim is very hard, but I'll try to persuade you. The task is made harder by the fact that for every possible Java program, there exists a C++ program that performs the exact same task at least as fast as Java (proof by example: the JVM). And yet, Java is the best performing programming environment in existence (well, in widespread use; there may be runtimes I'm not aware of). How is that possible? When we say that one language or environment is faster than another, do we mean that there exists a string of characters in that language that would produce a faster executable? If that is what we mean, then yes, C++ is faster than Java. But I think the question is not whether that desired program exists, but whether you can write it. To that question, I think the answer is no in most circumstances, especially when it comes to large programs. Why? because when you write a large, hopefully maintainable, C++ program, you must make use of language features that are hard for the compiler to optimize, most notably -- virtual methods. The JVM JIT does runtime compilation, so it inlines virtual calls, and de-optimizes them if its assumptions are shown to be false. Method inlining is the most important compiler optimization because it opens to door to all kinds of other optimizations like lock elision, scalarization of objects and more. And there's another issue. What do we mean by performance nowadays? It's not too hard for a single-threaded C++ program to beat a similar Java program (provided that the program is not large, and does not make use of virtual methods). But modern hardware is multi- and many-core, and most scalable lock-free algorithms absolutely require a good GC in order to function well. Java probably has (several of) the most advanced GC(s) in wide use. To sum up: C++ can beat Java in small benchmarks, but Java will beat C/C++ in most large, real-world programs; and if the program is heavily multi-threaded, Java has even more of an edge.
- beagle3 13y agoTaking your logic a little further, Python+PyPy beats Java hands down. Every single argument applies to Python vs. Java or Python vs. C/C++ more than it does to Java vs. C/C++ Which, incidentally, is my tool of choice... Personally, I've never met nice readable Java code. I'm sure it exists somewhere, but I've actually gone looking for it in the past (including posting a question on HN, out of 10 responses only one was concrete -- Play Framework -- and they've abandoned Java since). Every piece of Java I've met is an overengineered piece of AbstractAbstractFactoryFactoryBeanSingleton class. I'm sure Notch writes better code. But as far as I can tell he's the only one.
- qwerta 13y agoI work on java db engine. C/C++ and Java developers have completely different mind set. Where C guy is doing bite-wise operations Java guy will implement separate object for each pixel. It is rare to see tightly optimized java code. I see main Java advantage in concurrency and advanced programming. My code has fine-grained locking, robust, correct and very quickly written. And hand optimized code in bottlenecks usually has comparable speed.