Categories
Game Development

Pretty fast string class

I wrote my own string class, that is 5X faster than std::string for creation and 2X faster for copying. However, it is slower if I create a string that was previously deleted. I’m not sure what they are doing, but in that case creation is 2X faster than mine. It would be really cool if […]

I wrote my own string class, that is 5X faster than std::string for creation and 2X faster for copying. However, it is slower if I create a string that was previously deleted. I’m not sure what they are doing, but in that case creation is 2X faster than mine.

It would be really cool if I could get it faster in all cases.

Here’s the code:
RakString.cpp
RakString.h

Times are in milliseconds
Ref is just inserting a number, to show the speed of the list itself.
Rak is my string
Std is the std::string

Insertion 1 Ref=0 Rak=25, Std=102
RemoveHead Ref=106 Rak=4964, Std=8577
Insertion 2 Ref=1 Rak=22, Std=12
RemoveTail Ref=0 Rak=0, Std=0

Insertion 1 Ref=1 Rak=24, Std=102
RemoveHead Ref=107 Rak=4868, Std=8585
Insertion 2 Ref=0 Rak=23, Std=13
RemoveTail Ref=0 Rak=0, Std=0

Insertion 1 Ref=1 Rak=25, Std=104
RemoveHead Ref=107 Rak=4879, Std=8553
Insertion 2 Ref=0 Rak=22, Std=13
RemoveTail Ref=0 Rak=0, Std=0

Test code

static const int repeatCount=15000;
DataStructures::List rakStringList;
DataStructures::List stdStringList;
DataStructures::List referenceStringList;
unsigned i;
RakNetTime beforeReferenceList, beforeRakString, beforeStdString, afterStdString;

beforeReferenceList=RakNet::GetTime();
for (i=0; i < repeatCount; i++)
	referenceStringList.Insert(0);
beforeRakString=RakNet::GetTime();
for (i=0; i < repeatCount; i++)
	rakStringList.Insert("Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd");
beforeStdString=RakNet::GetTime();
for (i=0; i < repeatCount; i++)
	stdStringList.Insert("Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd");
afterStdString=RakNet::GetTime();
printf("Insertion 1 Ref=%i Rak=%i, Std=%i\n", 
beforeRakString-beforeReferenceList, 
beforeStdString-beforeRakString,
 afterStdString-beforeStdString);

beforeReferenceList=RakNet::GetTime();
for (i=0; i < repeatCount; i++)
	referenceStringList.RemoveAtIndex(0);
beforeRakString=RakNet::GetTime();
for (i=0; i < repeatCount; i++)
	rakStringList.RemoveAtIndex(0);
beforeStdString=RakNet::GetTime();
for (i=0; i < repeatCount; i++)
	stdStringList.RemoveAtIndex(0);
afterStdString=RakNet::GetTime();
printf("RemoveHead Ref=%i Rak=%i, Std=%i\n",
 beforeRakString-beforeReferenceList,
 beforeStdString-beforeRakString,
 afterStdString-beforeStdString);

beforeReferenceList=RakNet::GetTime();
for (i=0; i < repeatCount; i++)
	referenceStringList.Insert(0);
beforeRakString=RakNet::GetTime();
for (i=0; i < repeatCount; i++)
	rakStringList.Insert("Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd");
beforeStdString=RakNet::GetTime();
for (i=0; i < repeatCount; i++)
	stdStringList.Insert("Aalsdkj alsdjf laksdjf ;lasdfj ;lasjfd");
afterStdString=RakNet::GetTime();
printf("Insertion 2 Ref=%i Rak=%i, Std=%i\n",
 beforeRakString-beforeReferenceList,
 beforeStdString-beforeRakString,
 afterStdString-beforeStdString);

beforeReferenceList=RakNet::GetTime();
for (i=0; i < repeatCount; i++)
	referenceStringList.RemoveAtIndex(referenceStringList.Size()-1);
beforeRakString=RakNet::GetTime();
for (i=0; i < repeatCount; i++)
	rakStringList.RemoveAtIndex(rakStringList.Size()-1);
beforeStdString=RakNet::GetTime();
for (i=0; i < repeatCount; i++)
	stdStringList.RemoveAtIndex(stdStringList.Size()-1);
afterStdString=RakNet::GetTime();
printf("RemoveTail Ref=%i Rak=%i, Std=%i\n",
 beforeRakString-beforeReferenceList, 
beforeStdString-beforeRakString, 
afterStdString-beforeStdString);

Leave a Reply

Your email address will not be published. Required fields are marked *