Hello all,
I'm in a poition of trying to justify use of the STL from a performance
perspective. As a starting point, can anyone cite any benchmarks comparing
vectors to plain old statically-declared arrays? I'll also be looking at
the other STL containers later...
Also, I'd appreciate any comments anyone has on the suitability of the
little program below for comparing vectors and arrays. One obvious
shortcoming is that it uses array / vector indexing operations only as
lvalues, not as rvalues...
With VC++ 6.0 on a 1 GHz machine, this program produced the following
interesting results:
Release mode
---------------
Array: 109 seconds
Vector: 109 seconds
Debug mode
-------------
Array: 141 seconds
Vector: 1657 seconds
In debug mode, use of vectors is *not* justified, from a performance
perspective, by a factor of 11.75!
The identical release mode results are, I suppose, not surprising.
I'd love to hear people's thoughts or references to benchmarks more
scientific and appropriate than this little test!
Thanks,
Dave
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
namespace
{
const int NUM_ELEMENTS = 100000;
const int NUM_ITERS = 100000;
};
int main()
{
int a[NUM_ELEMENTS];
time_t finish;
int i;
int j;
int k = NUM_ELEMENTS / 2; // An arbitrary element to look up
time_t start;
vector<int> v(NUM_ELEMENTS);
// Test the array
start = time(NULL);
for (i = 0; i != NUM_ITERS; ++i)
{
for (j = 0; j != NUM_ELEMENTS; ++j)
a[j] = i + j;
}
// Try to inhibit any optimizations of the above loops.
++k;
cout << a[k] << endl;
finish = time(NULL);
cout << "Array implementation: " << difftime(finish, start) << endl;
// Test the vector
start = time(NULL);
for (i = 0; i != NUM_ITERS; ++i)
{
for (j = 0; j != NUM_ELEMENTS; ++j)
v[j] = i + j;
}
// Try to inhibit any optimizations of the above loops.
++k;
cout << v[k] << endl;
finish = time(NULL);
cout << "vector implementation: " << difftime(finish, start) << endl;
return 0;
} 12 9133
"Dave Theese" <ch**********@yahoo.com> wrote... I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks
comparing vectors to plain old statically-declared arrays? I'll also be looking at the other STL containers later... [...]
(a) Containers are NOT intended to outperform arrays. They have totally
different task altogether. They (just like many other things wrapped
in their own libraries) are designed to simplify development. That's
their primary designation. While they do have performance requirements
imposed on them in the C++ Standard, different implementations of the
standard library can display different results when it comes to some
kind of "performance testing".
(b) Small programs are NOT by any stretch of common sense a valid measure
for performance of something versus something in your _final_ product.
Comparing performances of different language constructs without the
rest of the system is blazing nonsense. Which brings on these two
recommendations:
(c) Do not optimise something whose speed you don't know. (And you can't
know until you have the entire system ready)
(d) Do not optimise something that although known as slow does not get
executed enough time to affect the overall performance of the system.
(again, you can't know that before you have the entire system)
If those things do not convince you to give up your attempts to find out
why and by how much containers are slower than arrays, use www.google.com
to search for whatever performance testing has already been done.
Victor
Dave Theese wrote: Hello all,
I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks comparing vectors to plain old statically-declared arrays? I'll also be looking at the other STL containers later...
HOLD IT.
I think I'm a performance freak and even I don't concern myself about
this issue unless I known I have a performance problem.
What are the requirements ?
Do you know that your application is performance constained ?
If you've been asked to go figure it out without constraints you've been
duped. I suggest you make some up that you know are good enough and you
and use the rule - lies, damn lies and statistics to prove whatever you
want. Also, I'd appreciate any comments anyone has on the suitability of the little program below for comparing vectors and arrays.
Your code tests accessing vector elements. It does not test object
creation and destruction or resizing, passing or other things that may
possibly be more of a constraint on your code.
Vector is a great general purpose tool. However, you can define more
specialized versions that will run considerably faster in *SOME* cases.
Again, it depends on the usage pattern in your application.
Come to think of it. Here is a technique you can use. Create a copy
(or a branch) of the codebase you currently have, run through the entire
code base and implement the vector version. You'll be surprised how
quickly you could do this. Maybe you can pick a library if it's a
really huge codebase. Then run a test on the real code. If you see more
than a 10% performance hit I'll be surprised, unless it's a very complex
app that creates and destoys vectors all over the place.
One obvious shortcoming is that it uses array / vector indexing operations only as lvalues, not as rvalues...
Likely not significant. With VC++ 6.0 on a 1 GHz machine, this program produced the following interesting results:
Release mode --------------- Array: 109 seconds Vector: 109 seconds
Debug mode ------------- Array: 141 seconds Vector: 1657 seconds
In debug mode, use of vectors is *not* justified, from a performance perspective, by a factor of 11.75!
But why do you care about debug performance ? The identical release mode results are, I suppose, not surprising.
I'd love to hear people's thoughts or references to benchmarks more scientific and appropriate than this little test!
If you have a heavy numerical library, there has been significant
developments. Look up C++ matrix libraries under google.
> Release mode --------------- Array: 109 seconds Vector: 109 seconds
Debug mode ------------- Array: 141 seconds Vector: 1657 seconds
Forget measuring the performance of debugging-code but incorporate
the times when the vector's capacity is about to change.
"Dave Theese" <ch**********@yahoo.com> wrote in message
news:vm************@news.supernews.com... Hello all,
I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks
comparing vectors to plain old statically-declared arrays? I'll also be looking at the other STL containers later...
An array is not a container, it doesn't have the option of using iterators
and it doesn't resize itself or grow in the background while your
application runs. If an array needs to grow, you need to:
a) generate a new larger array,
b) copy the contents from the old array,
c) copy the contents of the added data into the new array,
d) and possibly delete the old array from the heap.
or
e) use a vector and push_back. Done!
And it's initial size can even be set by a member function that you never
needed to define called reserve().
If what you need is an array, use an array. If what you need is an STL
container like a vector, the array is no match. Let's face it, do you know
excatly how small or big an array you'll need?
While a vector can only grow from the top, it can sort, pushback and even
insert objects anywhere because of iterators. Try doing that with an array.
Your vector is too small? Forget it, it grows by itself.
A deque, for example, can grow in both directions, iterate through objects
with the ++ operator, can sort, insert, pop low, pop high and push because
of iterators. A deque is a Double Ended QUEue container. The indiscutable
advantage is that, except for minor differences, once you learn how to
iterate through a vector, you have all the tools required to manage a deque,
or list, stack, queue, or priority_queue and you didn't do anything except
#include the container. Also, I'd appreciate any comments anyone has on the suitability of the little program below for comparing vectors and arrays. One obvious shortcoming is that it uses array / vector indexing operations only as lvalues, not as rvalues...
In other words, it's not so much that a vector is slower or faster than an
array when what you need is to manage a dynamically resizeable container's
objects or sequence. It's the cost of recreating the same behaviour with an
array that matters here. And once you have a working relationship with a
vector, using the other STL containers costs you little in programming time
because of standard behaviours all the STL containers abide by.
The cost of programming is infinitely more important than a few nanoseconds
a released executable will loose or gain. The STL containers are "free",
thoroughly tested reusable source code. With VC++ 6.0 on a 1 GHz machine, this program produced the following interesting results:
Release mode --------------- Array: 109 seconds Vector: 109 seconds
Debug mode ------------- Array: 141 seconds Vector: 1657 seconds
In debug mode, use of vectors is *not* justified, from a performance perspective, by a factor of 11.75!
The identical release mode results are, I suppose, not surprising.
I'd love to hear people's thoughts or references to benchmarks more scientific and appropriate than this little test!
Thanks, Dave
#include <iostream> #include <ctime> #include <vector>
using namespace std;
namespace { const int NUM_ELEMENTS = 100000; const int NUM_ITERS = 100000; };
int main() { int a[NUM_ELEMENTS]; time_t finish; int i; int j; int k = NUM_ELEMENTS / 2; // An arbitrary element to look up time_t start; vector<int> v(NUM_ELEMENTS);
// Test the array start = time(NULL);
for (i = 0; i != NUM_ITERS; ++i) { for (j = 0; j != NUM_ELEMENTS; ++j) a[j] = i + j; }
// Try to inhibit any optimizations of the above loops. ++k; cout << a[k] << endl;
finish = time(NULL); cout << "Array implementation: " << difftime(finish, start) << endl;
// Test the vector start = time(NULL);
for (i = 0; i != NUM_ITERS; ++i) { for (j = 0; j != NUM_ELEMENTS; ++j) v[j] = i + j; }
// Try to inhibit any optimizations of the above loops. ++k; cout << v[k] << endl;
finish = time(NULL); cout << "vector implementation: " << difftime(finish, start) << endl;
return 0; }
"Dave Theese" <ch**********@yahoo.com> wrote in message
news:vm************@news.supernews.com... With VC++ 6.0 on a 1 GHz machine, this program produced the following interesting results:
Release mode --------------- Array: 109 seconds Vector: 109 seconds
Debug mode ------------- Array: 141 seconds Vector: 1657 seconds
In debug mode, use of vectors is *not* justified, from a performance perspective, by a factor of 11.75!
Debug mode isn't about performance, though, so justification or otherwise of
vectors under debug mode is totally irrelevant, surely?
Paul
"Paul Thompson" <pa**@msdmagic.co.uk> wrote in message
news:bk**********@sparta.btinternet.com... "Dave Theese" <ch**********@yahoo.com> wrote in message news:vm************@news.supernews.com...
<snip>
Paul > Debug mode isn't about performance, though, so
justification or otherwise of
Paul > vectors under debug mode is totally irrelevant,
surely?
Unless debug mode is the mode that's released...
"SaltPeter" <Sa*******@Jupiter.sys> wrote in message news:<W9*******************@news20.bellglobal.com> ... "Dave Theese" <ch**********@yahoo.com> wrote in message news:vm************@news.supernews.com... Hello all,
I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks
comparing vectors to plain old statically-declared arrays? I'll also be looking at the other STL containers later...
If performance is an issue, I recommed you use a vector and access the
container via iterators.
I have perform test on static C-Style arrays and compare them with
std::vector containers, and the vector out performance the C-Style
array when the data is being access via iterator.
When the data is access via operator[], that's when you get mix
results.
Some implementations have vector out performming the C-Style array,
and some have the C-Style array out performming the vector. The
difference is usually hard to measure, so it's not that signaficant.
However, I haven't found any implementation in which the C-Style array
is able to out perform a vector via iterator.
"Dave Theese" <ch**********@yahoo.com> wrote in message news:<vm************@news.supernews.com>... int main() { int a[NUM_ELEMENTS]; time_t finish; int i; int j; int k = NUM_ELEMENTS / 2; // An arbitrary element to look up time_t start; vector<int> v(NUM_ELEMENTS);
There's a major difference here: The array 'a' is located on the
stack, while the contents of the vector are allocated from the heap.
Sam
"Dave Theese" <ch**********@yahoo.com> wrote in message news:<vm************@news.supernews.com>... Hello all,
I'm in a poition of trying to justify use of the STL from a performance perspective. As a starting point, can anyone cite any benchmarks comparing vectors to plain old statically-declared arrays? I'll also be looking at the other STL containers later...
Also, I'd appreciate any comments anyone has on the suitability of the little program below for comparing vectors and arrays. One obvious shortcoming is that it uses array / vector indexing operations only as lvalues, not as rvalues...
With VC++ 6.0 on a 1 GHz machine, this program produced the following interesting results:
Release mode --------------- Array: 109 seconds Vector: 109 seconds
Debug mode ------------- Array: 141 seconds Vector: 1657 seconds
In debug mode, use of vectors is *not* justified, from a performance perspective, by a factor of 11.75!
Check the STL documentation if vector in debug mode does index bounds
checking.
This would account for a 10 fold slow down on code like yours.
Get away from using raw arrays explicitly.
For a simple data structure vector is very hard to beat.
With the right compiler the blitz++ arrays library will do it but it
is certainly not simple. This is your best bet for vector - vector
operations in
small dimensions and stencils.
For linear algebra operations in larger dimensions the ATLAS BLAS is
your best bet. This one runs test suites during installation and
recompiles itself with different optimization parameters until it
finds good ones (can take hours).
There is a good paper on the net on the design (search for Jack
Donguerra and ATLAS). It is not efficient in very small dimensions
though (roughly <=20)
Dave> With VC++ 6.0 on a 1 GHz machine, this program produced the following
Dave> interesting results:
Dave> Release mode
Dave> ---------------
Dave> Array: 109 seconds
Dave> Vector: 109 seconds
Dave> Debug mode
Dave> -------------
Dave> Array: 141 seconds
Dave> Vector: 1657 seconds
Dave> In debug mode, use of vectors is *not* justified, from a performance
Dave> perspective, by a factor of 11.75!
The reason is that in debug mode, dynamically allocated storage
(such as is used by vectors) is initialized to a recognizable bit
pattern; in release mode, it isn't.
--
Andrew Koenig, ar*@acm.org
"Andrew Koenig" <ar*@acm.org> wrote in message news:yu**************@tinker.research.att.com... The reason is that in debug mode, dynamically allocated storage (such as is used by vectors) is initialized to a recognizable bit pattern; in release mode, it isn't.
Another factor is that debug mode inhibits inlining of functions which
the STL uses heavily, especially for things like operator[]. If you
go back and twiddle the compiler settings to allow inlining in debug
mode, I suspect it gets a LOT faster. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Cant Think Today |
last post by:
I have multi-dimesional arrays that can be specifed by the user, e.g
1,2,3,4,5
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6
I think a bit of code that will iterate over these arrays to print out
the...
|
by: Computer Whizz |
last post by:
I was reading through Accelerated C++ at work when I read through the first
mention of Vectors, giving us certain functions etc.
Is there any benefit of Arrays over Vectors? Since all Vectors...
|
by: Havatcha |
last post by:
Does anyone have a benchmark for the processing overhead of the STL
Vector class, vs a C style array?
I would dearly love to use Vectors, but am paranoid about slowing my
real-time code down. Can...
|
by: Ben |
last post by:
I have a program which is using a lot of memory.
At the moment I store a lot of pointers to objects in std::vector.
(millions of them)
I have three questions:
1) Lets say the average Vector...
|
by: Peter Olcott |
last post by:
I need to know how to get the solution mentioned below to work. The
solution is from gbayles Jan 29 2001, 12:50 pm, link is provided below:
>...
|
by: Peteroid |
last post by:
I looked at the addresses in an 'array<>' during debug and noticed that the
addresses were contiguous. Is this guaranteed, or just something it does if
it can?
PS = VS C++.NET 2005 Express...
|
by: John Bend |
last post by:
Hello.
Can anyone please suggest some good tutorial links where Java Vectors
and Arrays are explained and compared?
I'm a proficient programmer but fairly new to Java. Whilst I understand...
|
by: utab |
last post by:
Hi there,
Is there a way to convert a double value to a string. I know that there
is fcvt() but I think this function is not a part of the standard
library. I want sth from the standard if...
|
by: hyena |
last post by:
Hi,
I have a problem regarding passin 2 dimensional array into a function.
I have a to pass into function f(), f is called many times and the size
of a will change for each call.
I am not...
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |