Thread: which is more effecient?

  1. #16
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    It sounds like a vector would be faster since you can keep it sorted and use a binary search for finding things.
    p.s. What the alphabet would look like without q and r.

  2. #17
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Lucky, the vector code could probably be made faster if this:
    Code:
    VSETTINGS vset;
    
    for (int i = 0; i < items; ++i) {
        vset.name.push_back("ABCDEFG");
        vset.value.push_back("HIJKLMNOP");
    }
    Was changed to this:
    Code:
    VSETTINGS vset;
    
    vset.name.reserve(items);  // Reserve space in one fell swoop
    vset.value.reserve(items);  // Reserve space in one fell swoop
    
    for (int i = 0; i < items; ++i)
    {
        vset.name[i] = "ABCDEFG";
        vset.value[i] = "HIJKLMNOP";
    }
    This could change your timings, although I don't know by how much. Reserving that space initially would mean no reallocations of the vector memory. I haven't tested it but it seems like it should be faster. As has been said already, it all depends on how well you write your code.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #18
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Originally posted by hk_mp5kpdw
    Lucky, the vector code could probably be made faster...
    This could change your timings...
    Thanks for pointing that out, however, that would negate the purpose of the application. As you said it could change the timings which is why it would throw it off. The purpose of the code was to compare the time it would take to insert x number of items and to delete them. The way I figure it, a given user won't normally know the number of items to be inserted into a vector/list.

  4. #19
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    A few more points. Reserve grabs the space, but does not call any ctor's thus you still need to use push_back(). You could use resize() and []. structs can have =, ctor's, copy ctor's, and dtors just like classes, and if they are not provided, default ones are generated, just like classes. If all the members of a struct have working ctor's,=,etc.. then the default ones will work fine, just like classes. The only difference between a struct and a class is that a struct is born with no class and let's just anyone play with it's privates.

  5. #20
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Originally posted by grib
    The only difference between a struct and a class is that a struct is born with no class and let's just anyone play with it's privates.
    Hahahaha... Nice.

    As molestingly as grib put it, it's true that the only difference between a struct and class (depending on the compiler) is the default member type.

    "play with it's privates" hehehe

  6. #21
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    so structs can have functions? or did i completely misunderstand that? lol

  7. #22
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Originally posted by XSquared
    Because the vector class requires that the datatype have a defined assignment operator, which is not provided with structs.
    That would take about two seconds to make.

  8. #23
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Originally posted by the Wookie
    so structs can have functions? or did i completely misunderstand that? lol
    Yep, structs can have functions (in C++ at least). In C, it is a different story.

  9. #24
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    Originally posted by golfinguy4
    Yep, structs can have functions (in C++ at least). In C, it is a different story.
    oh ok i gotcha, but no constructor/destructor right?

  10. #25
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    structs are classes with a default access of public, that is the only difference.
    p.s. What the alphabet would look like without q and r.

  11. #26
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Originally posted by the Wookie
    so structs can have functions? or did i completely misunderstand that? lol
    Originally posted by me
    the only difference between a struct and class (depending on the compiler) is the default member type.
    Originally, structs were basically just groups of data combined with a common name. Eventually, compiler designers figured they were just so similar to classes that they are now handled identically, with the exception that the default member type of a struct is public and that of a class is private. Aside from that, everything you can do with a class you can also do with a struct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Effecient Code
    By computerquip in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2008, 09:57 PM
  2. Effecient Selection Statements?
    By 031003b in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2007, 06:08 AM
  3. Help making Program Better/More Effecient
    By toonlover in forum C++ Programming
    Replies: 14
    Last Post: 04-29-2006, 11:40 PM