Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark code comparing the custom String class with std::string is not an apples-to-apples comparison #8

Closed
azureskydiver opened this issue Mar 22, 2021 · 3 comments · Fixed by #11
Assignees

Comments

@azureskydiver
Copy link

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 an Allocator 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.

@LessComplexity
Copy link
Member

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?

LessComplexity added a commit that referenced this issue Mar 22, 2021
Add benchmark against same string implementation with new/delete and also std::string #8
@LessComplexity LessComplexity self-assigned this Mar 22, 2021
@azureskydiver
Copy link
Author

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.

@LessComplexity
Copy link
Member

Thanks again for your comment, I change the realloc() to a new then memcpy and then delete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants