From: Scott McPhillips [MVP] on 2 Oct 2009 09:37 "Eddards" <eddards(a)verizon.net> wrote in message news:Ts2dnaRpU_hEbVjXnZ2dnUVZ_tWdnZ2d(a)giganews.com... > This is what I have. > The size of the file read into buffer was 4.6mb > Why does it take so long to run through? (over a minute). > > int i; > CString CipStr; > for (i=0; i<buffer.GetSize(); i++){ > if (buffer[i] == 0x00) > buffer[i] = 0x01; > CipStr += buffer[i]; > } > It is slow because the CString has to repeatedly reallocate memory as it grows. That is much more time consuming that simply copying a character into an existing memory allocation. With CString::GetBuffer you can allocate the final size just once, before you start copying. That should produce a vast speed improvement. -- Scott McPhillips [VC++ MVP]
From: Joseph M. Newcomer on 2 Oct 2009 10:23 Because you did something truly silly: :you did +=. This means the second time through, it has to make a copy of a 1-character ClipStr; the third time through it has to make a copy of a 2-ClipStr ClipStr, the fourth time through it has to make a copy of a 3-character ClipStr, and each time it goes through it has to make a copy! This is called the "exponential copy problem", and you must have a very fast machine if it only took a minute. By the 4,200,001 iteration it was copying a string of 4,200,000 characters, then appending a character to that, then copying 4,200,001 characters and appending a character to that, and so on. This requires an allocation, which must be initialized, then a massive copy, only to, a few nanoseconds later, repeat this lengthy process. The CORRECT code would have been to run through and change all the bytes, and THEN make PRECISELY ONE copy, at the end! Note that you also failed in describing a problem: you didn't give all the critical information! String length is a critical parameter! for(int i = 0; i < buffer.GetSize(); i++) { if(buffer[i] = 0x00) buffer[i] = 0x01; } CString CipStr((LPCSTR)buffer.GetData(), buffer.GetSize()); This makes only one copy. joe On Fri, 2 Oct 2009 08:23:49 -0400, "Eddards" <eddards(a)verizon.net> wrote: >This is what I have. >The size of the file read into buffer was 4.6mb >Why does it take so long to run through? (over a minute). > > int i; > CString CipStr; > for (i=0; i<buffer.GetSize(); i++){ > if (buffer[i] == 0x00) > buffer[i] = 0x01; > CipStr += buffer[i]; > } > > >"Eddards" <eddards(a)verizon.net> wrote in message >news:KpidnfDBIMVReljXnZ2dnUVZ_uOdnZ2d(a)giganews.com... >>I tried copying the buffer with a loop like you show below. >> It took over a minute to process all 4,000,000 bytes. >> Dont know why it should take so long. >> >> >> "Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message >> news:57sac5hc63e673ijs0fdttud2annvadrno(a)4ax.com... >>> for(int i = 0; i < buffer.GetSize(); i++) >>> if(buffer[i] == 0x00) >>> buffer[i] = 0x01; >>> >>> What is so hard about this? This is pretty elementary C programming! >>> >>> If you want to do it more along the "hard way" read about the >>> MultiByteToWideChar API (and >>> see my essay on my MVP Tips site on CString techniques) >>> >>> joe >>> On Thu, 1 Oct 2009 15:09:07 -0400, "Eddards" <eddards(a)verizon.net> wrote: >>> >>>>I also need to replace any 0x00 (NULL) chars with 0x01 when copying to >>>>the >>>>CString >>>>Is this possible ? >>>> >>>>"Eddards" <eddards(a)verizon.net> wrote in message >>>>news:9fGdnWZaLPg3J1nXnZ2dnUVZ_u6dnZ2d(a)giganews.com... >>>>>I am using VC6. >>>>> Is there a way to copy a CByteArray to a CString? >>>>> >>>>> >>>> >>> Joseph M. Newcomer [MVP] >>> email: newcomer(a)flounder.com >>> Web: http://www.flounder.com >>> MVP Tips: http://www.flounder.com/mvp_tips.htm >> >> > Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Eddards on 2 Oct 2009 10:54 OK so now I have the following: for (i=0; i<buffer.GetSize(); i++){ if (buffer[i] == 0x00) buffer[i] = 0x01; } CString CipStr((LPCSTR)buffer.GetData(), buffer.GetSize()); Compiler error: error C2064: term does not evaluate to a function "Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message news:op2cc51p1oorq1s13h5g4us37ktgauv5nm(a)4ax.com... > Because you did something truly silly: :you did +=. This means the second > time through, > it has to make a copy of a 1-character ClipStr; the third time through it > has to make a > copy of a 2-ClipStr ClipStr, the fourth time through it has to make a copy > of a > 3-character ClipStr, and each time it goes through it has to make a copy! > This is called > the "exponential copy problem", and you must have a very fast machine if > it only took a > minute. By the 4,200,001 iteration it was copying a string of 4,200,000 > characters, then > appending a character to that, then copying 4,200,001 characters and > appending a > character to that, and so on. This requires an allocation, which must be > initialized, > then a massive copy, only to, a few nanoseconds later, repeat this lengthy > process. > > The CORRECT code would have been to run through and change all the bytes, > and THEN make > PRECISELY ONE copy, at the end! > > Note that you also failed in describing a problem: you didn't give all the > critical > information! String length is a critical parameter! > > for(int i = 0; i < buffer.GetSize(); i++) > { > if(buffer[i] = 0x00) > buffer[i] = 0x01; > } > > CString CipStr((LPCSTR)buffer.GetData(), buffer.GetSize()); > > This makes only one copy. > joe > > On Fri, 2 Oct 2009 08:23:49 -0400, "Eddards" <eddards(a)verizon.net> wrote: > >>This is what I have. >>The size of the file read into buffer was 4.6mb >>Why does it take so long to run through? (over a minute). >> >> int i; >> CString CipStr; >> for (i=0; i<buffer.GetSize(); i++){ >> if (buffer[i] == 0x00) >> buffer[i] = 0x01; >> CipStr += buffer[i]; >> } >> >> >>"Eddards" <eddards(a)verizon.net> wrote in message >>news:KpidnfDBIMVReljXnZ2dnUVZ_uOdnZ2d(a)giganews.com... >>>I tried copying the buffer with a loop like you show below. >>> It took over a minute to process all 4,000,000 bytes. >>> Dont know why it should take so long. >>> >>> >>> "Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message >>> news:57sac5hc63e673ijs0fdttud2annvadrno(a)4ax.com... >>>> for(int i = 0; i < buffer.GetSize(); i++) >>>> if(buffer[i] == 0x00) >>>> buffer[i] = 0x01; >>>> >>>> What is so hard about this? This is pretty elementary C programming! >>>> >>>> If you want to do it more along the "hard way" read about the >>>> MultiByteToWideChar API (and >>>> see my essay on my MVP Tips site on CString techniques) >>>> >>>> joe >>>> On Thu, 1 Oct 2009 15:09:07 -0400, "Eddards" <eddards(a)verizon.net> >>>> wrote: >>>> >>>>>I also need to replace any 0x00 (NULL) chars with 0x01 when copying to >>>>>the >>>>>CString >>>>>Is this possible ? >>>>> >>>>>"Eddards" <eddards(a)verizon.net> wrote in message >>>>>news:9fGdnWZaLPg3J1nXnZ2dnUVZ_u6dnZ2d(a)giganews.com... >>>>>>I am using VC6. >>>>>> Is there a way to copy a CByteArray to a CString? >>>>>> >>>>>> >>>>> >>>> Joseph M. Newcomer [MVP] >>>> email: newcomer(a)flounder.com >>>> Web: http://www.flounder.com >>>> MVP Tips: http://www.flounder.com/mvp_tips.htm >>> >>> >> > Joseph M. Newcomer [MVP] > email: newcomer(a)flounder.com > Web: http://www.flounder.com > MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Tom Serface on 2 Oct 2009 10:57 4M bytes in a string? Why do you want to move this to a CString? Maybe there is a better way of handling it. Still, if it's just memory to memory it shouldn't take that long. Do you have enough free memory on your machine? Tom "Eddards" <eddards(a)verizon.net> wrote in message news:KpidnfDBIMVReljXnZ2dnUVZ_uOdnZ2d(a)giganews.com... >I tried copying the buffer with a loop like you show below. > It took over a minute to process all 4,000,000 bytes. > Dont know why it should take so long. > >
From: Tom Serface on 2 Oct 2009 10:59 You may want to try setting the size of the CString since it's going to be so large. I'm not sure that would speed it up a lot, but it wouldn't hurt. You can get the size from the CByteArray. I don't think assigning it would be much quicker. Could you just read the file into the CString in the first place? You could still evalutate it byte by byte. Tom "Eddards" <eddards(a)verizon.net> wrote in message news:Ts2dnaRpU_hEbVjXnZ2dnUVZ_tWdnZ2d(a)giganews.com... > This is what I have. > The size of the file read into buffer was 4.6mb > Why does it take so long to run through? (over a minute). > > int i; > CString CipStr; > for (i=0; i<buffer.GetSize(); i++){ > if (buffer[i] == 0x00) > buffer[i] = 0x01; > CipStr += buffer[i]; > } >
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: FTPFindFirstFile unicode Next: AfxConnectionAdvise: No events fired |