Hi

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:

#include<iostream>

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)\
	StartMeasure();\
	for(unsigned __int64 _i = 0; _i < loops; _i++) {\
		event;\
	}\
	EndMeasure();\
	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   ";
	SpeedTest(
		SubStr(text, 0, 5),
	1000000);
	std::cout << "\tCPU Cycles";


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


	std::cin.ignore();
	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 :)

Thanks.

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:

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?

This article has been dead for over six months. Start a new discussion instead.