I am trying to make function which returns a substring as a char* , but string::substr returns a const char* . So I have managed to make a function that will return a substring as char* , except when I was comparing the speed of the two, I found that string::substr was much faster than the one I made. But its not actually to do with the technique I used to retrieve a substring, I found that just allocating the memory takes about twice as long than string::substr to manage everything.

Here is the code that compares the two functions:


unsigned __int64 start;
unsigned __int64 end;

inline __declspec(naked) unsigned __int64 GetCycleCount() {
    _asm rdtsc;
    _asm ret;
inline void StartMeasure() {
	start = GetCycleCount();
inline void EndMeasure() {
	end = GetCycleCount();
inline unsigned __int64 GetMeasure() {
	return (end - start);

#define SpeedTest(event, loops)\
	for(unsigned __int64 _i = 0; _i < loops; _i++) {\
	start /= loops, end /= loops;\
	std::cout << GetMeasure();

char *SubStr(char *text, unsigned int beg, unsigned int fin) {
	unsigned int len = fin - beg;
	char *sub = new char[len];
	memcpy(sub, &text[beg], len);
	sub[len] = '\0';
	return sub;

int main() {

	char text[] = "0123456789";
	std::string text_s = text;

	/*  SubStr  */
	std::cout << "\n SubStr:\t   ";
		SubStr(text, 0, 5),
	std::cout << "\tCPU Cycles";

	/*  string::substr  */
	std::cout << "\n\n string::substr:   ";
		text_s.substr(0, 5),
	std::cout << "\tCPU Cycles";

	return 0;


If anybody has any ideas on how to speed up my SubStr function / memory allocating to a similar speed as string::substr, please tell me :)


And one more question, when I run this example on Debug, I got the following output:

SubStr:            4073 CPU Cycles
string::substr:    689  CPU Cycles

However, if I run it on Release, I get this output:

SubStr:            3436  CPU Cycles
string::substr:    67    CPU Cycles

What is causing this difference in speed? :icon_rolleyes:

What is causing this difference in speed? :icon_rolleyes:

You might be interested in the amount of memory de/allocation operations that occur during the tests. Gathering basic statistics on malloc/free/realloc calls is quite simple by means of _CrtSetAllocHook(), see

Thanks for the reply mitrmkar,
I took a look at the link and that answers my second question. :)
Do you have any idea on how std::string / std::vector manages to allocate memory so quickly, if so, would it be possible to use the same technique here?