CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2009
    Location
    NY, USA
    Posts
    191

    Question about classes

    This is just an idea that I am trying to think whether would make sense or not. I could simulate what I am thinking and prove myself but I am looking for some logic behind it as well.

    So, I have lets say 20,000 rows of data and elements in each row refer to same variable i.e. price, date, time, quantity, etc.

    I have two options with it:
    Option 1:
    Create one object have a matrix to store all the rows and have methods defined to do calculations like finding total quantity in a certain time period etc.

    Option 2;
    I create a class with members like price, data, time, quantity, etc. Then I create 20,000 instances of these objects and have functions to do calculations like finding total quantity in a certain time period by calling each object and getting it's time and quantity.

    I think option 2 would be very slow as:
    - 20,000 objects need to be created
    - while getting quantity from each object i would need to have some sort of get function
    - option 1 is also faster as i created only one object and everything is stored in one nice table and if I need a value I just need to do something like MyTable[i][j] where MyTable is a vector<vector>

    Any thoughts?

  2. #2
    Join Date
    Jan 2009
    Posts
    596

    Re: Question about classes

    Quote Originally Posted by Learned View Post
    I think option 2 would be very slow as:
    - 20,000 objects need to be created
    - while getting quantity from each object i would need to have some sort of get function
    - option 1 is also faster as i created only one object and everything is stored in one nice table and if I need a value I just need to do something like MyTable[i][j] where MyTable is a vector<vector>

    Any thoughts?
    I very much doubt that either way would be too slow given the machine you are likely to be running it on (i.e. any PC/Mac built in the last ten years). And option 2 is the neater way to code this, as you have a class representing each individual item in the list. Depending upon your application you may need this class elsewhere anyway.

    I would say code it using option 2 and only worry about the speed if you find, when actually running the code, that this is too slow.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Question about classes

    Quote Originally Posted by Learned View Post
    I think option 2 would be very slow as:
    Never guess if something will be slow or fast, unless it can be measured algorithmically (i.e. a bubble sort versus a quicksort) or if it is obvious (for example, a loop that executes a million times instead of a thousand times using the same code in the loop)

    Except for those examples above, no programmer can measure accurately what will be slow or fast just by eyeballing C++ code, even though many have tried (and fail).

    C++ code that looks complex (usage of function objects, templates, etc.) look slow because the code uses modern techniques that seem to be bloated in terms of speed. But nothing could be further from the truth when you actually run the code with full optimizations turned on.

    Here is an example:
    Code:
    #include <vector>
    #include <algorithm>
    
    struct Test
    {
        std::string m_Class;
        int m_Date;
        int m_Quantity;
        int m_Type;
        int m_Price;
        Test() : m_Class("This is a long string of data"), m_Quantity(0), m_Type(0), m_Price(0) {}
    };
    
    typedef std::vector<Test> VecTest;
    
    struct AddMeUp
    {
        int *m_pTotal;
        AddMeUp(int *pTotal): m_pTotal(pTotal) { *m_pTotal = 0; }
        void operator()(const Test& t) { *m_pTotal += t.m_Price; }
    };
    
    void GetQuantity(const Test& t1)
    {
        t1.getQuantity();
    }
    
    int main()
    {
        VecTest v(200000);
        // total up m_Cost
        int total;
        std::for_each(v.begin(), v.end(), AddMeUp(&total));
        std::for_each(v.begin(), v.end(), GetQuantity);
    }
    This code, similar to what you described, took a fraction of a second to run on a (now old) Intel dual core processor, compiler used is VS 2008. This program created 200,000 (not just 20,000) objects, ran the total for all 200,000, and also called getQuantity() for all the objects.

    That is 10 times more objects than you mentioned, and it still took a fraction of a second to complete.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Question about classes

    Learned, you didn't state that precisely in your post, but your description sounds like the rows in your input file actually are lines in a text file.

    Assuming the above, I think what's most expensive is parsing and converting the string data to more concrete data types like integers, floats or dates. And avoiding the parsing when reading in the file and instead doing it later on demand would only gain anything at all if you're going to actually use only a minor part of the data in the file.

    If you still want to remain flexible in the data handling I'd suggest unconditionally using a class-based approach with a constructor (or perhaps factory method) that takes the string representing the row read from the input file as a parameter and does the parsing internally. Use accessors to get access to the data fields from the outside code instead of exposing the fileds directly. If parsing the string data on construction of the object actually should turn out to be too slow (which seems unlikely), you can switch to the parse-on-demand approach by just changing implementation details of the class, requiring no change to the outside code at all: Just have the cunstructor store the string it gets in an internal field and instead have the accessors parse the string on demand.

    Of course I second the points of the two posters before me. In particular Paul has made an important point: It's unlikely that your users even notice the difference between your code taking 1 ms or 100 ms to process the file.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured