Behavior of compilers.

S

Sharad Kala

Hi,

I want to know the behavior of modern C++ compilers for this short program.
Basically this tells us the rate by which vector grows.

#include <vector>
#include <iostream>
using namespace std;
int main ()
{
vector<int> v;
for (int i = 1; i <= 10; ++i)
{
v.push_back(i);
cout << v.capacity() << " ";
}
}

VC 7.0, 7.1 results - 1 2 3 4 6 6 9 9 9 13
g++ 3.3.1 results - 1 2 4 4 8 8 8 8 16 16

Can you share results of other major compiler vendors.

Thanks,
Sharad

P.S. - I know this has to to do with whether the allocator uses
first-fit/best-fit allocation strategy.
 
S

Sharad Kala

Sharad Kala said:
Hi,

I want to know the behavior of modern C++ compilers for this short program.
Basically this tells us the rate by which vector grows.

#include <vector>
#include <iostream>
using namespace std;
int main ()
{
vector<int> v;
for (int i = 1; i <= 10; ++i)
{
v.push_back(i);
cout << v.capacity() << " ";
}
}

VC 7.0, 7.1 results - 1 2 3 4 6 6 9 9 9 13
g++ 3.3.1 results - 1 2 4 4 8 8 8 8 16 16


Some more results (of course obtained by varying the loop counter) -
Borland 6 - 1 2 4 8 16 32 64 128 256 512 1024

aCC: HP ANSI C++ B3910B A.03.39
32 64 103 166 268 433 700 1132

Sun's CC 5.0
256 512 1024

-Sharad
 
D

Denis Remezov

Sharad said:
Hi,

I want to know the behavior of modern C++ compilers for this short program.
Basically this tells us the rate by which vector grows.

#include <vector>
#include <iostream>
using namespace std;
int main ()
{
vector<int> v;
for (int i = 1; i <= 10; ++i)
{
v.push_back(i);
cout << v.capacity() << " ";
}
}

VC 7.0, 7.1 results - 1 2 3 4 6 6 9 9 9 13
g++ 3.3.1 results - 1 2 4 4 8 8 8 8 16 16

Can you share results of other major compiler vendors.

icc 8.0 (Intel C++ Compiler, tested with its original library
on Linux; I heard it uses a combination of GNU and Dinkumware
libraries): 1 3 3 9 9 9 9 9 9 27
and so on beyond 10000000, i.e. power of 3.

Denis
 
R

Roshan

A long time back I read some reference to Don Knuth having researched into the
issue of the optimal growth factor for "general cases".
I believe it said his conclusion was that 1.5.
I guess optimal means not being too wasteful (of memory) and at the same time
something that doesnt require frequent
resizing.

From the results noted it looks like VC 7 (dinkumware really) is using 1.5 as its
resize factor.

I only vaguely recollect reading about Knuth's inference mentioned somewhere. So
i could be wrong.

-Roshan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top