vectors of floats

Discussion in 'C++' started by mp, Dec 27, 2005.

  1. mp

    mp Guest

    I am doing pairwise comparisons between 2 vectors of chars and
    permuting one vector and storing the resulting calculations in a
    vector<float> then I find a p-value among other stats. I have to do
    1,000,000 permuations for each pair, with a total of 2000 pairs to
    compare. Is a vector the appropriate container for such a job? Or is
    this a newbie mistake?

    Currently I am using a vector of pointers to an object which contains
    my pair of vectors and other data members, including 3 vectors which
    store 3 different stat calculations (added via push_back() ). This
    seems to be a poor choice as my memory usage goes through the roof and
    program crashes with more than 80 pairs to compare. I am a bit lost as
    what to do? I am not even sure what questions to ask. any ideas would
    be appreciated. Thanks, mp
     
    mp, Dec 27, 2005
    #1
    1. Advertising

  2. mp wrote:
    > I am doing pairwise comparisons between 2 vectors of chars and
    > permuting one vector and storing the resulting calculations in a
    > vector<float> then I find a p-value among other stats. I have to do
    > 1,000,000 permuations for each pair, with a total of 2000 pairs to
    > compare. Is a vector the appropriate container for such a job? Or is
    > this a newbie mistake?


    std::vector is the fastest when it comes to random access, and it's
    probably the best container for what you need, if you never shrink
    or grow it dynamically. If all you do is permutations (which simply
    moves elements around using assignment) and comparisons, std::vector
    is probably the best of all containers the Standard library has.

    > Currently I am using a vector of pointers to an object which contains
    > my pair of vectors and other data members, including 3 vectors which
    > store 3 different stat calculations (added via push_back() ). This
    > seems to be a poor choice as my memory usage goes through the roof and
    > program crashes with more than 80 pairs to compare.


    That is in itself an empty statement. Why is memory usage so high?
    Why does the program "crash"? Even if you use a lot of memory, your
    program is not supposed to "crash" (I presume you mean "abnormally
    terminate"). If one million permutations yields three million vectors
    and then each grows dynamically and unpredictably, that might present
    a challenge for your memory manager. However, there is always some
    way to know how much to allocate. So, try using 'reserve' member
    function to prevent the vectors from needing to reallocate. Your code
    will get (a) faster and (b) less memory-demanding.

    > I am a bit lost
    > as what to do? I am not even sure what questions to ask. any ideas
    > would be appreciated. Thanks, mp


    Read the FAQ 5.8.

    V
     
    Victor Bazarov, Dec 27, 2005
    #2
    1. Advertising

  3. "mp" <> wrote in message
    news:...
    > I am doing pairwise comparisons between 2 vectors of chars and
    > permuting one vector and storing the resulting calculations in a
    > vector<float> then I find a p-value among other stats. I have to do
    > 1,000,000 permuations for each pair, with a total of 2000 pairs to
    > compare. Is a vector the appropriate container for such a job? Or is
    > this a newbie mistake?
    >
    > Currently I am using a vector of pointers to an object which contains
    > my pair of vectors and other data members, including 3 vectors which
    > store 3 different stat calculations (added via push_back() ). This
    > seems to be a poor choice as my memory usage goes through the roof and
    > program crashes with more than 80 pairs to compare. I am a bit lost as
    > what to do? I am not even sure what questions to ask. any ideas would
    > be appreciated. Thanks, mp
    >


    You need to post some code so we can all fully understand your problem,
    especially with regard to the memory usage "going through the roof" !
    vector<> could well be the right structure to use, since if used
    appropriately,
    the accessing of elements can be virtually as efficient as using an array.
    Again post some code, and we can review it and suggest approaches.

    dave
     
    Dave Townsend, Dec 28, 2005
    #3
  4. mp

    mp Guest

    Dave Townsend wrote:
    > "mp" <> wrote in message
    > news:...
    > > I am doing pairwise comparisons between 2 vectors of chars and
    > > permuting one vector and storing the resulting calculations in a
    > > vector<float> then I find a p-value among other stats. I have to do
    > > 1,000,000 permuations for each pair, with a total of 2000 pairs to
    > > compare. Is a vector the appropriate container for such a job? Or is
    > > this a newbie mistake?
    > >
    > > Currently I am using a vector of pointers to an object which contains
    > > my pair of vectors and other data members, including 3 vectors which
    > > store 3 different stat calculations (added via push_back() ). This
    > > seems to be a poor choice as my memory usage goes through the roof and
    > > program crashes with more than 80 pairs to compare. I am a bit lost as
    > > what to do? I am not even sure what questions to ask. any ideas would
    > > be appreciated. Thanks, mp
    > >

    >
    > You need to post some code so we can all fully understand your problem,
    > especially with regard to the memory usage "going through the roof" !
    > vector<> could well be the right structure to use, since if used
    > appropriately,
    > the accessing of elements can be virtually as efficient as using an array.
    > Again post some code, and we can review it and suggest approaches.
    >
    > dave



    I seemed to have fixed my memory problem.

    I start with initializing a pointer to an object,

    Snpdata *q = NULL;

    for(int i = 0; i< newVectorSize ; i++) //load vector with enough
    pointers to my objects
    {
    q = new Snpdata();
    vecSnpdata.push_back(q);
    }


    I then load the objects with data from a file and run my stat
    functions.
    At the end of this I just delete the object after writing the results
    to a file.

    delete vecSnpdata;

    My program no longer aborts prematurely.
    Thank you,

    mp
     
    mp, Dec 28, 2005
    #4
  5. mp

    BobR Guest

    mp wrote in message
    <>...
    >
    >I seemed to have fixed my memory problem.
    >I start with initializing a pointer to an object,
    >
    > Snpdata *q = NULL;
    > for(int i = 0; i< newVectorSize ; i++) //load vector with enough
    >pointers to my objects
    > {
    > q = new Snpdata();
    > vecSnpdata.push_back(q);
    > }


    You can eliminate the temp pointer [ Snpdata *q(0) ]:

    for(int i(0); i< newVectorSize ; ++i){ // load vector with pointers to
    objects
    vecSnpdata.push_back( new Snpdata() );
    } // for(i)

    >At the end of this I just delete the object after writing the results
    >to a file.

    // >delete vecSnpdata;

    delete vecSnpdata.at( i );
    // use this, unless you are **positive** i is in range.

    --
    Bob R
    POVrookie
     
    BobR, Dec 28, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Koen
    Replies:
    4
    Views:
    404
    Sudsy
    Oct 27, 2004
  2. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,342
    Tim Rentsch
    Sep 23, 2005
  3. Replies:
    3
    Views:
    725
    Shadowman
    Mar 26, 2008
  4. someone
    Replies:
    26
    Views:
    908
    Jorgen Grahn
    Oct 24, 2011
  5. Guest
    Replies:
    0
    Views:
    481
    Guest
    Sep 14, 2005
Loading...

Share This Page