STL vs hand-coded

This is a discussion on STL vs hand-coded within the C++ Programming forums, part of the General Programming Boards category; Which will be more space-efficient and faster?...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    163

    STL vs hand-coded

    Which will be more space-efficient and faster?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Ussually the STL. And probably more reliant since many people have worked on it and tested it vs not many.
    Woop?

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    In my opinion that depends on how good you are. If you're really good programer than probably you can write more efficient algorithms regarding your data structures comapring with STL algorithms. But with flexibility that STL offer... that's a question. My advice is to stick to STL because you have very rich set of very powerful and well defined algorithms.

    - Micko
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Yeah - stick with the STL. It'll make it easier to work with other programmers, and unless you need to really save a couple of clock cycles by having a custom-built algorithm, there's no practical reason to avoid the concenience of STL.

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    I need to write a space efficient and fast algorithm.
    But I am not an expert in programming.
    But which is uses less space? A normal array or a STL vector?

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    It depends on the situation. If you know the exact size of the array you need beforehand, use an array so you can avoid the overhead. If you need variable-length arrays and the functions already built-in to vectors, use STL. The STL is space efficient and fast - there isn't a huge amount to gain by doing it yourself.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    > But which is uses less space? A normal array or a STL vector?
    If you use say reserve() to allocate the right amount up-front, it's going to be the same
    http://www.sgi.com/tech/stl/Vector.html

    > But I am not an expert in programming.
    In which case, the STL is likely to be hard to beat, and a lot of work on your part before you succeed.

    Premature optimisation is a terrible disease - get it working first, then ask how it might be improved.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    ok, thanks for your advice =)

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    163
    I just try a simple test. I assigned both STL vector and a normal array with 6 elements

    I initialize them by:

    <vector>int Arr(6); //vector

    int Arr(6); //normal array

    I used Valgrind to check how much space they consume. The vector took about 1600bytes while the normal array about 20 something bytes. I can't recall the exact amount.

  10. #10
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    I used Valgrind to check how much space they consume.
    Why didn't you just do that in the first place?

    The array would take up 24 bytes - 4 bytes per int. The vector takes up the same amount of space, plus all the space required to store the member functions. For large amounts of data, this overhead becomes negligible. We've helped you all you can - we don't know exactly what you're trying to do, so you need to make the call yourself now.

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,556
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    int main ( ) {
        vector<int> a1(6);
        int         a2(6);  // not an array, just an int initialised to 6
        int         a3[6];
        cout << a1.capacity() * sizeof(int) << endl;
        cout << sizeof(a2) << " " << a2 << endl;
        cout << sizeof(a3) << endl;
        return 0;
    }
    
    $ ./a.out
    24
    4 6
    24
    > The vector took about 1600bytes
    Maybe so, but that could well be down to the underlying allocator (which you have very little control over), so even if you did
    Code:
    int *myarray = new int[6];
    There's still no guarantee that it wouldn't allocate a larger amount behind your back.

    The point (or so you said) was to be comparitively efficient at the large scale. Minor differences in overhead will barely show up when you start allocating 1000's of these things.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    And of course the fact that a hand-coded version would come out in much higher bytes too.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  13. #13
    ima n00b, ok? orion-'s Avatar
    Join Date
    Aug 2005
    Location
    alberta, canada
    Posts
    55
    it depends on how good you are at programming. for example, the 'sqrt()' function. if you've come up with a better/faster algorithm of finding the square root of a number, then by all means, use it! if not, just stick to the sqrt() function provided by the STL.

  14. #14
    Registered User
    Join Date
    Aug 2005
    Posts
    10
    Quote Originally Posted by sean_mackrory
    Why didn't you just do that in the first place?

    The array would take up 24 bytes - 4 bytes per int. The vector takes up the same amount of space, plus all the space required to store the member functions. For large amounts of data, this overhead becomes negligible. We've helped you all you can - we don't know exactly what you're trying to do, so you need to make the call yourself now.
    Actually that is incorrect, member functions are not stored in instances of classes, the member function is actually turned into a function that takes a pointer to the object as a parameter.

    Code:
    class my_class
    {
    public:
    my_class(int val)
    {
    value = val;
    }
    int square()
    {
    return value*value;
    }
    private:
    int value;
    };
    
    int main()
    {
    my_class a(10),b(20),c(30);  //all take up sizeof(int) memory
    std::cout << a.square();  //square is invoked as shown below
    return 0;
    }
    This code gets translated by the compiler into something like this:
    Code:
    struct my_class
    {
    int value;
    };
    
    int square(my_class* this)
    {
    return this->value * this->value;
    }
    Therefore, no matter how many instances of my_class exist, there exists only one function to manipulate the member data.

    Furthermore, a vector of 6 integers would take up and additional (sizeof(int*) * 2) bytes, as the vector stores a pointer to the start of the data, a pointer to the end of the data, and a pointer to the end of the allocated space.

  15. #15
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I need to write a space efficient and fast algorithm.
    But I am not an expert in programming.
    But which is uses less space? A normal array or a STL vector
    The main speed problem with the vector is that it uses dynamic memory. If you don't want this to occur, and know the size of the array, then rolling out your own using a template parameter ought to execute slightly faster. The STL algorithms can be still be use by forming a forward iterator using the beginning and ending of the array. (I don't think backward iterators work but you might check your documentatino)

    Therefore, no matter how many instances of my_class exist, there exists only one function to manipulate the member data.
    ComputerPhreak, yea, I think perhaps he's confusing virtual member functions with just plain member functions.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Formatting Using STL
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2004, 04:52 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. STL or no STL
    By codec in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2004, 02:36 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21