4 ms·
Can you explain to someone who has not written a kernel but still coded Java and C(++) for 20 years how this quote is possible (how it works that Java which is
by bullen 5y ago
Can you explain to someone who has not written a kernel but still coded Java and C(++) for 20 years how this quote is possible (how it works that Java which is written in C can outperform C):
"While I'm on the topic of concurrency I should mention my far too brief chat with Doug Lea. He commented that multi-threaded Java these days far outperforms C, due to the memory management and a garbage collector. If I recall correctly he said "only 12 times faster than C means you haven't started optimizing"." - Martin Fowler https://martinfowler.com/bliki/OOPSLA2005.html https://martinfowler.com/bliki/OOPSLA2005.html
I get the feeling this quote is possible due to the memory management rewrite of the entire JVM in version 1.5 to allow Doug to make the concurrency package?
But I cannot explain it, even if I have experienced it!
- tialaramex 5y agoI can try, in the most general sense. There are some specific technical tricks going on like the use of a JIT (Just In Time compiler) but mostly it comes back to this: The Java program isn't doing anything that is literally impossible in C, but powerful abstractions can make it exponentially easier for everybody to do it in Java. I can just about keep in my head how Quick Sort works. So, I figure I could write one, from scratch, in maybe a day or two in a language like C and have it work. But obviously I shouldn't do that, your standard C library comes with a perfectly nice fast sort. However, Java is supplied with an array of equally good features, and C is quite sparse on that front. You could program every one of those features in C, but, you probably didn't. Memory management is an example like that, but spread through most programs. If you're meticulously allocating and freeing things in C, that takes time. Is it worth it? Maybe not but can you keep track well enough to do anything faster without just leaking unlimited memory? Java comes with a good Garbage Collector which can avoid all that explicit tracking you need to do, yet free all the same memory. Clever. You can do this in C, some people do, but it's hard, you probably don't do it.
- bullen 5y agoI only use hardcoded arrays in C, that way it's hard to leak anything and you avoid cache misses, but I feel fortunate I use Java on the server because I would not be able to sleep with C crashing on there! I think the real technical reason, like you allude to, is closer to this quote: "Many lock-free structures offer atomic-free read paths, notably concurrent containers in garbage collected languages, such as ConcurrentHashMap in Java. Languages without garbage collection have fewer straightforward options, mostly because safe memory reclamation is a hard problem..." - Travis Downs https://travisdowns.github.io/blog/2020/07/06/concurrency-costs.html https://travisdowns.github.io/blog/2020/07/06/concurrency-co... But I have yet to meet someone (4 years into this realization) that can clearly explain why in a way that anyone could understand and more importantly accept!