|
Optimization results
>> File writing under windows98 win32 and msvc++ compiler. <<
On average I found win32 file writing to be 5x faster than C. C is 3x faster than C++ STL(dinkum that comes with vc++6). STL is 20x slower than win32.
-------
My results from console app(writing to file over and over):
Writing 10000 integers 10000 times
C file writing took: 9.675209 seconds
STL file writing took: 33.263714 seconds
Win32 file writing took: 1.730036 seconds
Press any key to continue
------
If you use vector.reserve() and then vector = value then it''s only 2.3x slower than "C" array filling. If vector.push_back() is used (constantly allocates from heap during looping) then it''s 16x slower than "C" array filling. Now for the kicker, if using either iterators or vector.size() to iterate the vector then it''s faster than "C" array iteration.
------(using reserve())
C array: 0.041174 seconds
STL array: 0.096588 seconds
iterate C array: 0.047839 seconds
iterate STL array: 0.000004 seconds
Press any key to continue
------
----(using push_back())
C array: 0.040835 seconds
STL array: 0.638112 seconds
iterate C array: 0.042605 seconds
iterate STL array: 0.060424 seconds
Press any key to continue
-------
Maybe no one is interested
Wrote 40,000 chars to stream 10,000 times in Java 2 i.e. v1.3 and it took 50 seconds. Same test as above.
So it's slower than STL file io.
Edited by - JD on July 10, 2001 1:25:05 AM

Wrote 40,000 chars to stream 10,000 times in Java 2 i.e. v1.3 and it took 50 seconds. Same test as above.
|
So it's slower than STL file io.
Edited by - JD on July 10, 2001 1:25:05 AM
> Now for the kicker, if using either iterators or vector.size() to iterate the vector then it's faster than "C" array iteration.
And why is that surprising? You are NOT doing the same test, they can't be compared!
Replace your Interate C array by:
And you'll probably see no difference with STL.
Y.
Edited by - Ysaneya on July 11, 2001 4:48:31 AM
And why is that surprising? You are NOT doing the same test, they can't be compared!
Replace your Interate C array by:
|
And you'll probably see no difference with STL.
Y.
Edited by - Ysaneya on July 11, 2001 4:48:31 AM
July 10, 2001 03:36 AM
I think there''s a bug in your code...when ever it says "press any key to continue" only the down arrow or the page down key works, maybe its just my HTML interepter (Netscape 4.5 on RH Linux 6.2 with KDE) who knows. The frame rate''s not too bad though, keep up the good work.
Hope this helps in the development of you article
Hope this helps in the development of you article

Yeah
that "Press blah..." is direct copy from console, too lazy to edit.
I''ll be dropping STL in exchange of my own code. I can beat vector insertions when the reserved size is the same for both my and stl code. Push_back is simply too slow even with reserved space. It''s even slower when you don''t reserve the space in the vector. There''s also some funky stuff going on in stl. If I reserve space in vector then use iterator to assign values and then want to print vector elements I think I have to use .capacity() or .size() doesn''t do anything. Basically nothing works until I use .push_back().
Here''s my own dynamically allocated and resized array code that beats pants of STL if you reserve all space for items in the array. If you reserve on 1/10 of what you reserve for STL then the the my code and stl code is even in speed. Try it yourself

I''ll be dropping STL in exchange of my own code. I can beat vector insertions when the reserved size is the same for both my and stl code. Push_back is simply too slow even with reserved space. It''s even slower when you don''t reserve the space in the vector. There''s also some funky stuff going on in stl. If I reserve space in vector then use iterator to assign values and then want to print vector elements I think I have to use .capacity() or .size() doesn''t do anything. Basically nothing works until I use .push_back().
Here''s my own dynamically allocated and resized array code that beats pants of STL if you reserve all space for items in the array. If you reserve on 1/10 of what you reserve for STL then the the my code and stl code is even in speed. Try it yourself

|
Ysaneya,
I'm filling the array with ints. Maybe I didn't understand your code can you get back to me?
The following should be the same(then again I don't know if the assembler differs them
I just don't have the time to figure out the pecularities of STL and test each and every single function and find out how to drive the STL optimally. If I don't care about speed I can probably see a use for STL but not in games where every cycle could be used to put more trees or stuff into the game. I'm using small set of STL so rewrite of this code is not that bad.
My observation of STL tells me that using .reserve() and not using .push_back() but instead using *iterator = value or vector = i is faster. Still you can out code STL if you tried. I only hope I screwed up somewhere in my thinking and coding so that STL in reality is faster than my code, so if you know a way could you provide a hint please
Basically for me it goes like this.
From fastest to slowest code:
1)Use operating system api functions (file writing is really fast, possibly other stuff too)
2)Use C library
3)Use C++ STL possibly third party libs
4)Use assembly in parts(maybe a first choice but is time consuming, using this only when I absolutely must)
Hope I don't have to put on my asbestos gear. Any thoughts? Are you timing your code? What's your observation of STL? Any websites out there provide timing results of vc++ stl vs. hand rolled code? Thanks.
Edited by - JD on July 10, 2001 12:29:29 AM
I'm filling the array with ints. Maybe I didn't understand your code can you get back to me?
The following should be the same(then again I don't know if the assembler differs them
|
I just don't have the time to figure out the pecularities of STL and test each and every single function and find out how to drive the STL optimally. If I don't care about speed I can probably see a use for STL but not in games where every cycle could be used to put more trees or stuff into the game. I'm using small set of STL so rewrite of this code is not that bad.
My observation of STL tells me that using .reserve() and not using .push_back() but instead using *iterator = value or vector = i is faster. Still you can out code STL if you tried. I only hope I screwed up somewhere in my thinking and coding so that STL in reality is faster than my code, so if you know a way could you provide a hint please

Basically for me it goes like this.
From fastest to slowest code:
1)Use operating system api functions (file writing is really fast, possibly other stuff too)
2)Use C library
3)Use C++ STL possibly third party libs
4)Use assembly in parts(maybe a first choice but is time consuming, using this only when I absolutely must)
Hope I don't have to put on my asbestos gear. Any thoughts? Are you timing your code? What's your observation of STL? Any websites out there provide timing results of vc++ stl vs. hand rolled code? Thanks.
Edited by - JD on July 10, 2001 12:29:29 AM
I did one more test to make sure I wasn''t halucinating when I say STL is slower than my own code. I reserved both arrays to max and filled them with ints. My code is 4 times faster than STL code.
I had to use push_back() on stl vector any other way to fill the vector would yield zero vector''s size and I couldn''t write the ints to the console. Using b[h] = value resulted in b.size() == zero but the elements did wrote themselves to the console when I used h < max instead of h < b.size(). Capacity in both cases was max elements. Looks like dinkum''s stl is slow. I may try the SGI stl library but I hear stl is not very cross platform compliant so I''ll probably stick with writing my own containers.
|
I had to use push_back() on stl vector any other way to fill the vector would yield zero vector''s size and I couldn''t write the ints to the console. Using b[h] = value resulted in b.size() == zero but the elements did wrote themselves to the console when I used h < max instead of h < b.size(). Capacity in both cases was max elements. Looks like dinkum''s stl is slow. I may try the SGI stl library but I hear stl is not very cross platform compliant so I''ll probably stick with writing my own containers.
JD: sorry, i''ve been confusing the tags with another site, check out the code in my previous reply now.
Basically, in your iterate C array, you are doing 2 additions in your loop (one for the array, one to increment the index). In your interate STL array, you are doing only one (incrementing a pointer). So no wonder why it''s faster in the second test!
Y.
Basically, in your iterate C array, you are doing 2 additions in your loop (one for the array, one to increment the index). In your interate STL array, you are doing only one (incrementing a pointer). So no wonder why it''s faster in the second test!
Y.
quote:
Original post by JD
I just don't have the time to figure out the pecularities of STL and test each and every single function and find out how to drive the STL optimally. If I don't care about speed I can probably see a use for STL but not in games where every cycle could be used to put more trees or stuff into the game. I'm using small set of STL so rewrite of this code is not that bad.
You see, I find this to be an, er, interesting view to say the least. It almost always takes longer to recode something yourself than to work out how to use an existing tool properly, providing you have good references on hand. A lot of people here on GameDev can point out the optimal way to use something, and a little study of the SGI STL documentation helps too. Also, perhaps buying a book? (I hear the Josuttis one is good.) You're right that saving cycles is important, but evaluating code in isolation is rarely too useful. What's more important is to profile the end result, because if it turns out that STL vector routines constitute less than 1% of your runtime CPU usage, then you've wasted your time rewriting it when you could have been optimising other code.
As for STL not being useful in games... the idea of pretty much any library is to get the job done quickly and reliably. Then, you optimise once you know what needs optimising. 99% of the time, taking twice as long for an array iteration is not going to be that big of a deal. Besides, most people would use the operator[] on vectors rather than iterators, which will probably optimise better, since many STL vectors are implemented as C arrays anyway! In which case, you've lost nothing in terms of iterator speed, and gained in terms of flexibility (functions to check the number of elements, automatic reallocation, etc).
Going back to what I said about reliability: you seem to require people to iterate through your array by using that public member pointer, yes? That doesn't seem like a too robust solution if other programmers had to use it. That pointer could get changed in subtle ways with just a simple typing error in the code. And on a second point, look at your destructor:
if(!m_pArray)delete [] m_pArray;}
Ooh, look: only delete the array if m_pArray is NULL. I guess that means it'll never get deleted. Nice memory leak, and I bet you wouldn't have found it for quite a while, since everything 'works'. This is another reason why people use the STL: because it's been extensively tested already....
Last points: your tests are not scientifically valid. Apart from potential issues such as too many additions or whatever Ysaneya was mentioning, there are other problems. Your tests suffer from what are called 'order effects'. Such things affect all scientific studies, from psychology to computing. Basically, performing a set of tests in a given order can have an effect on the results. For instance, in your filing test, by the time the Win32 API test comes around, the Win32 API functions have already been called numerous times (by stdio and iostreams) and are likely to be in the CPU cache, and hence the results are likely to be better than if you did them in a different order. Or did them in separate programs. Additionally: did you use any form of optimisation for these tests? The STL contains a lot of code that gets optimised away in the final version of the executable, which you might not have been taking into account. And which version of the compiler did you use? And did you patch the iostream bugs with the fixes on the Dinkumware site? Without revealing all these factors, your tests are almost meaningless. Sorry

quote:
I had to use push_back() on stl vector any other way to fill the vector would yield zero vector's size
This is because you slightly misunderstand what 'reserve' does. It doesn't say "Ok, this vector is now X in size" . It just says "Ok, now we have enough memory allocated so that the vector can grow to X in size before we need to allocate any more memory." See the difference? It means that you can't just write to the vector after you've reserved it. Reserve() is just a request for memory, not a resizing of the vector. Maybe you should try with the 'vector(size_type n, const T& t)' constructor, eg: vector<int> intss(COUNT, 0); which will give you a vector with 'COUNT' as the size right from the start. (Note that the 2nd parameter to the constructor is the value to initialise all the items to, and is optional. If omitted, they are left uninitialised.)
Anyway, I hope I've cleared up a few things there. Good luck with whichever method you choose, but don't write STL off just yet.
Edited by - Kylotan on July 11, 2001 8:51:24 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement