473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Behavior of compilers.

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.
Jul 22 '05 #1
3 1173

"Sharad Kala" <no******************@yahoo.com> wrote in message
news:2m************@uni-berlin.de...
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
Jul 22 '05 #2
Sharad Kala wrote:

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
Jul 22 '05 #3
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

Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DaKoadMunky | last post by:
<CODE> #include <iostream> using namespace std; int Foo(int x,int y) { int result = x; result*=y; result+=y;
13
by: Generic Usenet Account | last post by:
Compile the following snippet of code and run it. If the program spits out bat:bat instead of bat:zat, what would you say? Would you say that the compiler has a problem, or would you lay the...
15
by: shablool | last post by:
Hi all, Two distinct compilers give different output for the following code: #include <iostream> #include <string> int main(void) { std::string s("0124"); s.replace(0, 3,...
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
3
by: MarkN | last post by:
modf() is a C Standard Library function double modf(double x, double* ip); returns fractional part and assigns to *ip integral part of x, both with same sign as x I have encountere a situation...
5
by: Peter | last post by:
In my code, taking a "sizeof (DF_Header_Data_t)" on the structure below returns 48 when it should be 44. Why? typedef struct DF_Header_Data_t { int fbNumEntries; int fbNumFrequencies; double...
7
by: thamizh.veriyan | last post by:
Hi, I am new to this community. I have a doubt regarding trap representations. I read in IBM's website that something like this is legal: int main(){ int x=3; {
4
by: er | last post by:
hi, the code below generates the following behavior. cld someone please help understand it? 1) clean project + build project generates build errors (see bottom) 2) build a second time, errors...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.