Java collections faster than c++ containers? -
i reading comments on answer , saw quote.
object instantiation , object-oriented features blazing fast use (faster c++ in many cases) because they're designed in beginning. , collections fast. standard java beats standard c/c++ in area, optimized c code.
one user (with high rep might add) boldly defended claim, stating
heap allocation in java better c++'s
and added statement defending collections in java
and java collections fast compared c++ collections due largely different memory subsystem.
so question can of true, , if why java's heap allocation faster.
this sort of statement ridiculous; people making either incredibly uninformed, or incredibly dishonest. in particular:
the speed of dynamic memory allocation in 2 cases depend on pattern of dynamic memory use, implementation. trivial familiar algorithms used in both cases write benchmark proving ever 1 wanted faster. (thus, example, programs using large, complex graphs build, torn down , rebuilt, typically run faster under garbage collection. programs never use enough dynamic memory trigger collector. programs using few, large, long lived allocations run faster manual memory management.)
when comparing collections, have consider in collections. if you're comparing large vectors of
double, example, difference between java , c++ slight, , go either way. if you're comparing large vectors ofpoint,pointvalue class containing 2 doubles, c++ blow java out of water, because uses pure value semantics (with no additional dynamic allocation), java needs dynamically allocate eachpoint(and no dynamic allocation faster fastest dynamic allocation). ifpointclass in java correctly designed act value (and immutable,java.lang.string), doing translation onpointin vector require new allocation everypoint; in c++, assign.much depends on optimizer. in java, optimizer works perfect knowledge of actual use cases, in particular run of program, , perfect knowledge of actual processor running on, in run. in c++, optimizer must work data profiling run, never correspond 1 run of program, , optimizer must (usually) generate code run (and run quickly) on wide variety of processor versions. on other hand, c++ optimizer may take more time analysing different paths (and effective optimization can require lot of cpu); java optimizer has quick.
finally, although not relevant applications, c++ can single threaded. in case, no locking needed in allocator, never case in java.
with regards 2 numbered points: c++ can use more or less same algorithms java in heap allocator. i've used c++ programs ::operator delete() function empty, , memory garbage collected. (if application allocates lots of short lived, small objects, such allocator speed things up.) , second: big advantage c++ has memory model doesn't require dynamically allocated. if allocation in java takes tenth of time take in c++ (which case, if count allocation, , not time needed collector sweeps), large vectors of point, above, you're comparing 2 or 3 allocations in c++ millions of allocations in java.
and finally: "why java's heap allocation faster?" isn't, necessarily, if amortise time collection phases. time allocation can cheap, because java (or @ least java implementations) use relocating collector, results in of free memory being in single contiguous block. @ least partially offset time needed in collector: contiguity, you've got move data, means lot of copying. in implementations, means additional indirection in pointers, , lot of special logic avoid issues when 1 thread has address in register, or such.
Comments
Post a Comment