Thread: Efficiency question...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Efficiency question...

    I have made two classes that use basically the same exact ways of doing things, but I cant seem to figure out which one is faster to use.
    I am only going to show my variables for the classes cause there are alot of prototypes in the classes.

    Code:
    class MusicPlayer
    {
    public:
        //Functions
    
    private:
        //Variables
        std::map<std::string, std::string> name_;
        std::map<std::string, int> songnumber_;
        std::map<std::string, int> songlengthMS_;
        
        //other vars
    };
    and this is the other class
    Code:
    class MusicPlayer
    {
    public:
        //Functions
    private:
        //Variables
        std::vector<std::string> name_;
        std::vector<int> songnumber_;
        std::vector<int> songlengthMS_;
        
        //other vars
    };
    Questions:
    1) Which is faster
    2) Which is (opinion) more memory efficient
    3) Is there maybe a even better way to do this?
    those are the two classes. I want to figure out which one is faster when using a relatively large amount of files. ( between 300-1000 ). Thank you for any assistance or help you may provide.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Those don't do the same things, so which one is faster depends on what you plan to do with the data. Also, which is is better depends on a lot more than which one is faster.

    For name_, are you going to do some sort of lookup to see if the name exists? Are you going to have some sort of playlist order? How are you going to use the name_ variable?

    For songnumber_, is this related to the song's name? Does the songnumber need to be kept connected to its name or vice versa?

    Same thing for songlength.

    One thought is that you might consider combining song information into a class and store instances of that class in your MusicPlayer. Among other things, this will make things easier when you want to store things like author, genre, etc.

    Regardless, you still need to indicate how you want to use this data. What data needs to be kept together, what data will need to be looked up, etc.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 1) Which is faster
    Overall, or did you have a specific class API (like search) in mind?
    Without a list of your class member functions and some indication of which matter to you, or how you intend to use them, it's meaningless to ask "which is faster"

    > 2) Which is (opinion) more memory efficient
    Load up some data, then use the memory diagnostics of your machine to find out.

    > 3) Is there maybe a even better way to do this?
    There's always a better way, but without a hell of a lot more information about what the class is supposed to do, saying what it might be is pure guesswork.

    Get some real files, and start profiling with real data.
    Anything else is just meaningless guesswork.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It may be better to create something like:
    Code:
    struct Song
    {
        std::string name;
        int song_no;
        int length;
    };
    (That's programming-wise. As for efficiency, it doesn't probably matter, because you are not going to do anything computationally expensive with the songlists and they are relatively small after all? So choose whatever expresses your intentions.)
    Last edited by anon; 04-04-2007 at 01:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM