Because you also have to allocate memory for whatever variables a vector has internally.
Because you also have to allocate memory for whatever variables a vector has internally.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
It doesn't make program that slow.
[edit]
Does it? How many variables it allocates for each push_back()?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Everything depends on your usage, but vector is not generally slower by any significant amount than a dynamic array.
I agree with you. I'm just suggesting reasons for the cannon statement.It doesn't make program that slow.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
There are very few.
If the code is too complex, vectors should be used to ease coding, debugging and maintenance.
If the code is too simple, vectors should be used because they are still simpler than an array to implement and we already have a head start in case we need to upgrade the code.
Arrays should be used on the internals of complex classes or hierarchies where the speed factor is critical. The differences in speed are minimal... So only when minimal has a real meaning, should they be used. And needless to say if we need some of the facilities provided by the algorithms, we have to at least be as good programmers as the library implementators to be able to code them with arrays in mind.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
While I agree with you for the most part, the standard algorithms generally work with arrays, too.
I am comparing dynamically allocated memory to vectors. Static arrays are faster of course.
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
A vector cannot be (if it is, the library implementator is deaf) implemented just to give access to its elements. It must do it in Constant Time ( O(1) ). Any performance hit can only be the result of the constraints derived from the fact a vector is indeed implemented on top of an array.
But because it is still implemented in constant time and because the hit is very small, only with big amounts of data or with the need to traverse its contents repeatedly an incredible number of times can it become noticeable.
And then probably the difference in time on my computer is not asnearly significant on yours, if your processor is just a nudge as faster than mine.
In the end, really... no. They have the same speed. They run both at constant time
EDIT: And you are right Daved. Forgot about the fact many of the algorithms can work with arrays.
Last edited by Mario F.; 08-24-2006 at 06:01 PM.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
The only other thing I use arrays for is buffers and the like which are filled by API function calls. Then I immediately create a vector (or string) and delete[] the buffer.Originally Posted by Mario F.
I'm nervous about letting an API call directly copy memory into a vector. I know the standard guarantees it should work without problems, though, at least for vector and string (but none of the other containers). I always considered it bad form, though.
Last edited by Cat; 08-24-2006 at 06:59 PM.
You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.
Wow.
You guys really know your stuff.
Before yesterday I didn't even know what a pointer was, or a vector, or any of the things you're talking about. I actually did browse through some of those while trying to find a way for this to work on my own. But I get it better now having read this, and I don't think I could have figured out how to use them without this board.
The program compiles now. But I have a new problem.
Our CS department uses Linux computers. The logins from last year have been erased and the administrator hasn't given us new ones yet, so I wrote my program on a Windows computer in the school library on something called 'VisualStudio.net'. When I try to run my program I get an error message saying the debugger for Visual Studio wasn't properly installed, even if I tell it to start the program without debugging.
So I have a program which compiles but I don't know if it actually does what I want. There are about six computers with Visual Studio on them. I'll try a few of those and see if they work instead.
Thanks for all the help, by the way.
If I posted
>> I'm nervous about letting an API call directly copy memory into a vector. I know the standard guarantees it should work without problems, though, at least for vector and string (but none of the other containers).
Only for vector, string has no such guarantee.
>> So I have a program which compiles but I don't know if it actually does what I want.
You can try Ctrl-F5 to execute the program, or you can open a command prompt, find your exe, and execute it from there.
Or compile in release mode to prevent it from inserting debug symbols in your app.
Huh. Guess I learned something new today.Originally Posted by Daved
You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.