Jump to content
  • Advertisement
Sign in to follow this  
jwein

Vector STL Implementation

This topic is 3937 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I' m going to write the core classes of my graphics engine but, during the development of a vector-like class I' ve discovered that my class is much slower than the original one... o.o My class has a templated pointer and a push_back() function which allocates a block if there's not enough memory and writes the value... I' ve written a test-application and... The std::vector class writes 50000 elements in 1 second while my implementation does it in 15 seconds o.o What could be the problem ? Thanks, Bye !

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by jwein
during the development of a vector-like class

why would you do this.

Share this post


Link to post
Share on other sites
Most commonly used implementations of the STL are highly optimised and you should rarely expect to be able to write anything that's faster unless you know specifically why an STL container is going to be sub-optimal for a given situation.

In this case there are likely a number of reasons why std::vector is faster than your implementation of a vector-like container. I suspect a main killer is the way that your STL vector implementation resizes itself campared to you vector-like container.
Many STL implmentations will resize proportionally based on the current capacity by a factor of 2, so it doubles its capacity, giving you a linear time operation when performing repeated push_backs, if you were to only increase the capacity by a constant amount then it will be a hefty quadratic time operation.

Not to mention, in the STL, allocation and construction are separated tasks; giving fast and efficient allocation and JIT (Just-In-Time) construction of elements.

Share this post


Link to post
Share on other sites
Im guessing that you call sleep(14000) in the vector constructor.

What? Its a valid guess unless I see the code. [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by jwein
a push_back() function which allocates a block if there's not enough memory and writes the value... while my implementation does it in 15 seconds o.o
What could be the problem ?


Are you by any chance increasing the allocation size by just 1 each time (i.e. just enough room to hold the new element)? std::vector is smarter than that :)

Share this post


Link to post
Share on other sites
Hey, no i' ve solved the problem there was a bug in the programming. :)
I' m writing my class because i don' t like to use code of third part except for the graphics api :)
My class is faster than the original with lots of elements and it has an equal performance for little loops...
Sorry for my bad english but i'm italian :)

Share this post


Link to post
Share on other sites
Quote:

' m writing my class because i don' t like to use code of third part except for the graphics api :)

std::vector isn't a third-party library.

Quote:

My class is faster than the original with lots of elements and it has an equal performance for little loops...

Somehow I doubt this.

Share this post


Link to post
Share on other sites
It's not hard to beat STL in speed, especially the STL in 2005 which got slower than in 2003 due to the checked iterator crap being enabled even in release. He should probably make sure he's comparing the speed to the STL with that disabled.

Share this post


Link to post
Share on other sites
Quote:


Quote:

My class is faster than the original with lots of elements and it has an equal performance for little loops...

Somehow I doubt this.


I too find this very unlikely. Especially if it has gone from a 15 sec pause inducing bug to a Stroustrup and co. out performing class. But if this is truly the case I think that we NEED to see that code... so we can all use it.

BTW I was in the same boat as you about not wanting to use other's code for a long time. I have come around on the stl, not using it is like shooting yourself in the foot. Its cross platform, and truly part of C++.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!