Closed azureskydiver closed 3 years ago
Interesting Idea.
I think I will just create another implementation of String
which uses the regular new & delete and compare it to the implementation with the current string class, what do you say?
You can't mix in calling the standard C realloc()
to reallocate a memory block that has been allocated using new
. Yes, it may work with some C/C++ runtime implementations, but there is no guarantee that it always will, specially if the user override the global new
with their own implementation.
Thanks again for your comment, I change the realloc()
to a new
then memcpy
and then delete
Most C++ implementations of
std::string
will allocate a new block when appending a string (unless the implementation is specifically tuned to always allocate extra space after a string assignment); and then the data from the old block is copied to the new block, and the old block is freed. The memory pool implementation doesn't have to allocate a new block. It just extends the current block and so it gains the speed advantage.It would be a fairer benchmark if the
String
class were templated and it took anAllocator
where the allocator could either be the standard C++ allocator, or the MemoryPool allocator. That way it can be shown that the same operations are being performed, but MemoryPool is actually faster.