Click here to Skip to main content
15,917,062 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: File filter on default save/open dialog Pin
Paul Ranson25-Mar-04 5:56
Paul Ranson25-Mar-04 5:56 
GeneralCalling a text file Pin
Caoimh25-Mar-04 4:57
Caoimh25-Mar-04 4:57 
GeneralRe: Calling a text file Pin
David Crow25-Mar-04 5:19
David Crow25-Mar-04 5:19 
GeneralCFileDialog: change current selection extension when selecting new filter Pin
MyttO25-Mar-04 4:39
MyttO25-Mar-04 4:39 
Generalformat a cstring for time Pin
BlackDice25-Mar-04 4:35
BlackDice25-Mar-04 4:35 
GeneralRe: format a cstring for time Pin
MyttO25-Mar-04 4:51
MyttO25-Mar-04 4:51 
GeneralRe: format a cstring for time Pin
Paul Ranson25-Mar-04 5:34
Paul Ranson25-Mar-04 5:34 
GeneralRe: format a cstring for time Pin
Joe Woodbury25-Mar-04 5:39
professionalJoe Woodbury25-Mar-04 5:39 
CString will do only one realloc, which is time consuming, and then each insert will require a memcpy for the remainder of the string, which is fairly slow.

After playing around, out of curiosity, I came up with the following, which is about 2.2 times faster than doing a series of inserts. I also observed an additional 10 fold increase by ensuring that the CString always had at least 20 bytes allocated. (Avoiding an alloc/realloc made a huge difference.)

char dstStr[20];
const char* pSrc = orgStr;
dstStr[0] = pSrc[0];
dstStr[1] = pSrc[1];
dstStr[2] = pSrc[2];
dstStr[3] = pSrc[3];
dstStr[4] = '-';
dstStr[5] = pSrc[4];
dstStr[6] = pSrc[5];
dstStr[7] = '-';
dstStr[8] = pSrc[6];
dstStr[9] = pSrc[7];
dstStr[10] = ' ';
dstStr[11] = pSrc[8];
dstStr[12] = pSrc[9];
dstStr[13] = ':';
dstStr[14] = pSrc[10];
dstStr[15] = pSrc[11];
dstStr[16] = ':';
dstStr[17] = pSrc[12];
dstStr[18] = pSrc[13];
dstStr[19] = 0;


[EDIT: Another method, though this assumed that CString will be preallocated to 20 bytes and will remain that. Otherwise its performance degrades to near that of the insert method:

char* pBuffer = orgStr.GetBuffer(20);

pBuffer[18] = pBuffer[13];
pBuffer[17] = pBuffer[12];
pBuffer[15] = pBuffer[11];
pBuffer[14] = pBuffer[10];
pBuffer[12] = pBuffer[9];
pBuffer[11] = pBuffer[8];
pBuffer[9]  = pBuffer[7];
pBuffer[8]  = pBuffer[6];
pBuffer[6]  = pBuffer[5];
pBuffer[5]  = pBuffer[4];

pBuffer[4] = '-';
pBuffer[7] = '-';
pBuffer[10] = ' ';
pBuffer[13] = ':';
pBuffer[16] = ':';

orgStr.ReleaseBuffer(19);


Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke

GeneralRe: format a cstring for time Pin
BlackDice25-Mar-04 5:54
BlackDice25-Mar-04 5:54 
GeneralRe: format a cstring for time Pin
Robert A. T. Káldy25-Mar-04 11:23
Robert A. T. Káldy25-Mar-04 11:23 
GeneralRe: format a cstring for time Pin
BlackDice25-Mar-04 11:46
BlackDice25-Mar-04 11:46 
GeneralFont Symbols Pin
Reunion25-Mar-04 4:28
Reunion25-Mar-04 4:28 
GeneralHowTo clear a radio button selection Pin
Caoimh25-Mar-04 3:05
Caoimh25-Mar-04 3:05 
GeneralRe: HowTo clear a radio button selection Pin
22491725-Mar-04 3:37
22491725-Mar-04 3:37 
GeneralRe: HowTo clear a radio button selection Pin
David Crow25-Mar-04 3:43
David Crow25-Mar-04 3:43 
GeneralRe: HowTo clear a radio button selection Pin
Antony M Kancidrowski25-Mar-04 3:47
Antony M Kancidrowski25-Mar-04 3:47 
GeneralRe: HowTo clear a radio button selection Pin
Antony M Kancidrowski25-Mar-04 3:49
Antony M Kancidrowski25-Mar-04 3:49 
Generalmsxml get element line / position Pin
Brian van der Beek25-Mar-04 3:05
Brian van der Beek25-Mar-04 3:05 
GeneralRe: msxml get element line / position Pin
Ravi Bhavnani25-Mar-04 6:33
professionalRavi Bhavnani25-Mar-04 6:33 
GeneralRe: msxml get element line / position Pin
Brian van der Beek25-Mar-04 20:42
Brian van der Beek25-Mar-04 20:42 
GeneralServices and system environment vars Pin
sagmam25-Mar-04 3:02
sagmam25-Mar-04 3:02 
Generalhandling CString in unicode Pin
Manikandan25-Mar-04 2:33
Manikandan25-Mar-04 2:33 
GeneralRe: handling CString in unicode Pin
David Crow25-Mar-04 2:37
David Crow25-Mar-04 2:37 
GeneralRe: handling CString in unicode Pin
_Magnus_25-Mar-04 2:40
_Magnus_25-Mar-04 2:40 
GeneralRe: handling CString in unicode Pin
David Crow25-Mar-04 2:42
David Crow25-Mar-04 2:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.