Board index » cppbuilder » Heap vs vector vs alloc

Heap vs vector vs alloc


2006-09-28 02:30:34 AM
cppbuilder80
I am running programs that use variables which change in size with different
usage. The program reads in a file that tells it the size of these variable
and using vectors I then create the space for them. This is a program where
timing is critical. I on rare occasion get acess violation errors and the
program just goes into an endless error loop.
Is there a better way to be doing this? Thanks, DK
for example
NumberOfISI=TempString.SubString(1,p=TempString.Pos(",")-1).ToInt();
TempString = CommandList->Strings[y];++y; //ISI
for(int i=0;i<NumberOfISI;++i)
{
ISI.push_back(ustemp);
ISI[i]=TempString.SubString(1,p=TempString.Pos(",")-1).ToDouble();
TempString.Delete(1,p+1);
}
.......lots of other stuff
NumberOfTrials=(NumberOfXY*NumberOfXY*NumberOfISI);
Trial TempTrial;
for( i =0; i<NumberOfTrials+NumberOfCatchTrials;++i)
{
for(int j = 0; j< MaximumNumberOfSubjects;++j)
{
TrialList.push_back(TempTrial); // add a trial element
TrialList[i].SubjectResponse[j] = " 0 ";
TrialList[i].SubjectRT[j] = " 0 ";
}
Index.push_back(ustemp);
Index[i] = i;
}
 
 

Re:Heap vs vector vs alloc

Quote
...Is there a better way to be doing this?...
"better" is a subjective word. I have it on the authority of my wife that I
often don't know how to do things "better", so let's just call this an
"idea".
Allocation calls take a bit of time. From a quick look at that code, the
program knows about how many push_back's it's going to do prior to entering
the loop. Before entering the loop you might want to first call something
like this:
vector_name.reserve(
vector_name.size() +
the_number_to_add +
possibly_a_small_fudge_factor_just_in_case);
. Ed
Quote
DKat wrote in message
news:451ac347$ XXXX@XXXXX.COM ...

I am running programs that use variables which change in size with
different usage. The program reads in a file that tells it the size of
these variable and using vectors I then create the space for them. This is
a program where timing is critical. I on rare occasion get acess violation
errors and the program just goes into an endless error loop.

Is there a better way to be doing this? Thanks, DK


for example

NumberOfISI=TempString.SubString(1,p=TempString.Pos(",")-1).ToInt();
TempString = CommandList->Strings[y];++y; //ISI
for(int i=0;i<NumberOfISI;++i)
{
ISI.push_back(ustemp);
ISI[i]=TempString.SubString(1,p=TempString.Pos(",")-1).ToDouble();
TempString.Delete(1,p+1);
}

.......lots of other stuff
NumberOfTrials=(NumberOfXY*NumberOfXY*NumberOfISI);
Trial TempTrial;
for( i =0; i<NumberOfTrials+NumberOfCatchTrials;++i)
{
for(int j = 0; j< MaximumNumberOfSubjects;++j)
{
TrialList.push_back(TempTrial); // add a trial element
TrialList[i].SubjectResponse[j] = " 0 ";
TrialList[i].SubjectRT[j] = " 0 ";
}
Index.push_back(ustemp);
Index[i] = i;
}
 

Re:Heap vs vector vs alloc

Firstly, I agree with Ed. Optimizing the number of
allocations/deallocations can make a huge difference in execution time.
Quote
NumberOfISI=TempString.SubString(1,p=TempString.Pos(",")-1).ToInt();
Unless 'TempString' *always* has a comma you should perform some sanity
checking before using the return value of Pos. If the if a comma was not
found then the return value will be 0 making the SubString parameters
(0, -1). This will assuredly result in either an empty string or an AV (not
sure if it has an internal range check). But the call to ToInt will
definitely AV in this case.
Quote
ISI[i]=TempString.SubString(1,p=TempString.Pos(",")-1).ToDouble();
Again, a possible AV point for the same reason mentioned above.
Quote
Index.push_back(ustemp);
Index[i] = i;
Just a suggestion here. I see no reason to use 'ustemp' rather than simply
pushing 'i'.
HTH,
- Clayton
 

{smallsort}

Re:Heap vs vector vs alloc

Thank you for the tips. The comma comment is critical and I now have that
check in the code as well as losing the ustemp. In the rush to getting
things up and running I'm often flying by the seat of my pants. Once the
code get established then the sloppiness gets perpetuated.
"Clayton Arends" < XXXX@XXXXX.COM >wrote in message
Quote
Firstly, I agree with Ed. Optimizing the number of
allocations/deallocations can make a huge difference in execution time.

>NumberOfISI=TempString.SubString(1,p=TempString.Pos(",")-1).ToInt();

Unless 'TempString' *always* has a comma you should perform some sanity
checking before using the return value of Pos. If the if a comma was not
found then the return value will be 0 making the SubString parameters
(0, -1). This will assuredly result in either an empty string or an AV
(not sure if it has an internal range check). But the call to ToInt will
definitely AV in this case.

>ISI[i]=TempString.SubString(1,p=TempString.Pos(",")-1).ToDouble();

Again, a possible AV point for the same reason mentioned above.

>Index.push_back(ustemp);
>Index[i] = i;

Just a suggestion here. I see no reason to use 'ustemp' rather than
simply pushing 'i'.

HTH,
- Clayton


 

Re:Heap vs vector vs alloc

Thank you. This is the type of thing that I have never come across and is
exactly what I should be doing.
It is always a pleasure to find such things.
"Ed Mulroy" < XXXX@XXXXX.COM >wrote in message
Quote
>...Is there a better way to be doing this?...

"better" is a subjective word. I have it on the authority of my wife that
I often don't know how to do things "better", so let's just call this an
"idea".

Allocation calls take a bit of time. From a quick look at that code, the
program knows about how many push_back's it's going to do prior to
entering the loop. Before entering the loop you might want to first call
something like this:

vector_name.reserve(
vector_name.size() +
the_number_to_add +
possibly_a_small_fudge_factor_just_in_case);

. Ed

>DKat wrote in message
>news:451ac347$ XXXX@XXXXX.COM ...
>
>I am running programs that use variables which change in size with
>different usage. The program reads in a file that tells it the size of
>these variable and using vectors I then create the space for them. This
>is a program where timing is critical. I on rare occasion get acess
>violation errors and the program just goes into an endless error loop.
>
>Is there a better way to be doing this? Thanks, DK
>
>
>for example
>
>NumberOfISI=TempString.SubString(1,p=TempString.Pos(",")-1).ToInt();
>TempString = CommandList->Strings[y];++y; //ISI
>for(int i=0;i<NumberOfISI;++i)
>{
>ISI.push_back(ustemp);
>ISI[i]=TempString.SubString(1,p=TempString.Pos(",")-1).ToDouble();
>TempString.Delete(1,p+1);
>}
>
>.......lots of other stuff
>NumberOfTrials=(NumberOfXY*NumberOfXY*NumberOfISI);
>Trial TempTrial;
>for( i =0; i<NumberOfTrials+NumberOfCatchTrials;++i)
>{
>for(int j = 0; j< MaximumNumberOfSubjects;++j)
>{
>TrialList.push_back(TempTrial); // add a trial element
>TrialList[i].SubjectResponse[j] = " 0 ";
>TrialList[i].SubjectRT[j] = " 0 ";
>}
>Index.push_back(ustemp);
>Index[i] = i;
>}