4 ms·
Java has a ridiculously fast new operator. Much faster than the one in C++. It works by allocating all or most needed memory at program start, instead of askin
by Shorel 3y ago
Java has a ridiculously fast new operator. Much faster than the one in C++.
It works by allocating all or most needed memory at program start, instead of asking the operating system for it every time.
But, as soon as you don't use heap memory, and use the stack, C++ is again much, much faster than Java.
It all depends on memory management.
- seanhunter 3y agoC++'s new operator can also do this using "placement new" semantics[1], which usually means making (and using) a custom allocator. You're still using the normal new operator, just passing extra parameters to do the placement you want. To your second point about raw speed, one of the extraordinary things we found in long-running processes performing computation using real-time marketdata at Goldman was that the HotSpot JVM was able to optimize java programs through the day due to their usage, so if you started them each day they would actually end up faster than the C++ versions at the end of the day even though they would start off slower. That's not due to memory allocation it's due to things like inlining of functions. The implication of that is that if you very carefully inlined all the functions appropriately in the C++ version based on profiling actual usage you would be able to achieve the same result, but for the JVM it just happens automatically without you doing anything. [1] Search https://en.cppreference.com/w/cpp/language/new https://en.cppreference.com/w/cpp/language/new for "Placement New"
- pkolaczk 3y ago> Java has a ridiculously fast new operator. Much faster than the one in C++. Can you point us to some benchmarks? I don't doubt it is faster, but I doubt the "ridiculously" part. Last time I measured it was only a tad faster than jemalloc (~20%) if you allowed the benchmark to run long enough for GC to start cleaning up. Unfortunately I haven't saved it (it was a very informal benchmark). I'm curios to see that esp. on modern low-pause GCs.
- the-smug-one 3y agoIt's just pointer bumping (still). If you're continously using new memory then soft page faults and finding physical backing via kernel will dominate. mallocsen want to avoid this scenario, so you often see malloc using more memory than necessary. Anyway, don't use new in C++ is my opinion.
- pkolaczk 3y agoIt's pointer bumping in optimistic case. Eventually it runs out of current slab and... then there is some more work to do. And also those page faults are actually quite likely as it gives you memory that hasn't been used recently. Malloc tends to return often whatever was released last that matches the requested allocation size. It is indeed a tad more work to find it, but when it's found it's usually hot in the cache.