Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Access performance : array, vector, basic_string

0 views
Skip to first unread message

Alex Vinokur

unread,
Mar 25, 1999, 3:00:00 AM3/25/99
to

Hi,

Performance of accesses to elements
in C-array,
STL vector and
STL basic_string
was measured.

Average time-cost of execution was obtained.
The results of the experiment are shown below.

Main conclusions :
1. char_array < char_string < char_vector;
2. int_array < int_vector << int_string.
Note! The "<" symbol means "faster".
The "<<" symbol means "much faster".

3. The faster hierarchies are different
for *int* and *char* data structures (Why?)

4. Access to basic_string<unsigned int>
takes a lot of time (Why?)

Thanks,
Alex

P.S. Here are the results of the experiment.


//########################################################
//------------------- C++ code : BEGIN -------------------

//=================================================
#include <string>
#include <vector>
#include <sys/time.h>

//=======================
void print_status (
int testNo_i,
hrtime_t& t_start_i,
hrtime_t& t_end_i,
int array_size_i,
int iters_i,
const string& name_i
)
{
cout << "Test#"
<< testNo_i
<< " ["
<< name_i.c_str ()
<< "]"
<< "\t: "
<< "\tAverage time = "
<< (((t_end_i - t_start_i)/array_size_i)/iters_i)
<< " nsec"
<< endl;
}
//=======================
#define PRINT(testNo_i, object_i) \
print_status ( \
testNo_i, \
time_start, \
time_end, \
ARRAY_SIZE, \
TOTAL_Iterations, \
#object_i)

//####################################################
int main()
{
//=======================
const unsigned int TOTAL_Tests = 5;
const unsigned int TOTAL_Iterations = 10000;
const unsigned int ARRAY_SIZE = 500;

//=======================
hrtime_t time_start;
hrtime_t time_end;
//=======================
unsigned int int_array [ARRAY_SIZE];
vector<unsigned int> int_vector;
basic_string<unsigned int> int_string;
//----------
char char_array [ARRAY_SIZE];
vector<char> char_vector;
string char_string;
//=======================

unsigned int curTestNo;
unsigned int curIndex;
unsigned int curIteration;
unsigned int intValue = 25;
char charValue = 'Z';

for (curIndex = 0;
curIndex < ARRAY_SIZE;
curIndex++)
{
//========
int_array [curIndex] = intValue;
int_vector.push_back (intValue);
int_string += intValue;
//========
char_array [curIndex] = charValue;
char_vector.push_back (charValue);
char_string += charValue;
//========
}

//======================

for (curTestNo = 1;
curTestNo <= TOTAL_Tests;
curTestNo++)
{
//=======================
cout << endl
<< "\t"
<< "##### Test#"
<< curTestNo
<< " (access)"
<< " #####"
<< endl;

//############ int #################
//=======================================
//======= int_array ================
time_start = gethrtime();
for (curIteration = 1;
curIteration <= TOTAL_Iterations;
curIteration++)
{
for (curIndex = 0;
curIndex < ARRAY_SIZE;
curIndex++)
{
int_array [curIndex];
}
}
time_end = gethrtime();
PRINT (curTestNo, int_array);
//===============================

//===================================
//======= int_vector ================
time_start = gethrtime();
for (curIteration = 1;
curIteration <= TOTAL_Iterations;
curIteration++)
{
for (curIndex = 0;
curIndex < ARRAY_SIZE;
curIndex++)
{
int_vector [curIndex];
}
}
time_end = gethrtime();
PRINT (curTestNo, int_vector);
//===============================


//===================================
//======= int_string ================
time_start = gethrtime();
for (curIteration = 1;
curIteration <= TOTAL_Iterations;
curIteration++)
{
for (curIndex = 0;
curIndex < ARRAY_SIZE;
curIndex++)
{
int_string [curIndex];
}
}
time_end = gethrtime();
PRINT (curTestNo, int_string);
//===============================


cout << endl;
//############ char #################
//=======================================
//======= char_array ================
time_start = gethrtime();
for (curIteration = 1;
curIteration <= TOTAL_Iterations;
curIteration++)
{
for (curIndex = 0;
curIndex < ARRAY_SIZE;
curIndex++)
{
char_array [curIndex];
}
}
time_end = gethrtime();
PRINT (curTestNo, char_array);
//===============================

//===================================
//======= char_vector ================
time_start = gethrtime();
for (curIteration = 1;
curIteration <= TOTAL_Iterations;
curIteration++)
{
for (curIndex = 0;
curIndex < ARRAY_SIZE;
curIndex++)
{
char_vector [curIndex];
}
}
time_end = gethrtime();
PRINT (curTestNo, char_vector);
//===============================


//===================================
//======= char_string ================
time_start = gethrtime();
for (curIteration = 1;
curIteration <= TOTAL_Iterations;
curIteration++)
{
for (curIndex = 0;
curIndex < ARRAY_SIZE;
curIndex++)
{
char_string [curIndex];
}
}
time_end = gethrtime();
PRINT (curTestNo, char_string);
//===============================

} // for (curTestNo = 1; curTestNo <= TOTAL_Tests; curTestNo++)
//======================
return 0;
}

//------------------- C++ code : END ---------------------


//#########################################################
//------------------- Running Results : BEGIN -------------


##### Test#1 (access) #####
Test#1 [int_array] : Average time = 90 nsec
Test#1 [int_vector] : Average time = 354 nsec
Test#1 [int_string] : Average time = 1076 nsec

Test#1 [char_array] : Average time = 103 nsec
Test#1 [char_vector] : Average time = 422 nsec
Test#1 [char_string] : Average time = 262 nsec

##### Test#2 (access) #####
Test#2 [int_array] : Average time = 84 nsec
Test#2 [int_vector] : Average time = 362 nsec
Test#2 [int_string] : Average time = 1078 nsec

Test#2 [char_array] : Average time = 83 nsec
Test#2 [char_vector] : Average time = 422 nsec
Test#2 [char_string] : Average time = 277 nsec

##### Test#3 (access) #####
Test#3 [int_array] : Average time = 86 nsec
Test#3 [int_vector] : Average time = 365 nsec
Test#3 [int_string] : Average time = 1234 nsec

Test#3 [char_array] : Average time = 108 nsec
Test#3 [char_vector] : Average time = 445 nsec
Test#3 [char_string] : Average time = 266 nsec

##### Test#4 (access) #####
Test#4 [int_array] : Average time = 83 nsec
Test#4 [int_vector] : Average time = 359 nsec
Test#4 [int_string] : Average time = 1160 nsec

Test#4 [char_array] : Average time = 135 nsec
Test#4 [char_vector] : Average time = 437 nsec
Test#4 [char_string] : Average time = 300 nsec

##### Test#5 (access) #####
Test#5 [int_array] : Average time = 94 nsec
Test#5 [int_vector] : Average time = 344 nsec
Test#5 [int_string] : Average time = 1160 nsec

Test#5 [char_array] : Average time = 106 nsec
Test#5 [char_vector] : Average time = 440 nsec
Test#5 [char_string] : Average time = 255 nsec

//------------------- Running Results : END ---------------

//#########################################################
//------------------- Compiler & System ------------------

g++ -v : gcc version egcs-2.91.57 19980901
(egcs-1.1 release)

uname -a : SunOS <nodename> 5.6 Generic_105181-09
sun4m sparc SUNW,SPARCstation-5

psrinfo -v : -> The sparc processor operates at 110 MHz

//---------------------------------------------------------


//#########################################################

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Chris Newton

unread,
Mar 26, 1999, 3:00:00 AM3/26/99
to
Alex Vinokur wrote...

>Performance of accesses to elements
> in C-array,
> STL vector and
> STL basic_string
>was measured.
>
>Average time-cost of execution was obtained.
>The results of the experiment are shown below.
>
>Main conclusions :
> 1. char_array < char_string < char_vector;
> 2. int_array < int_vector << int_string.
> Note! The "<" symbol means "faster".
> The "<<" symbol means "much faster".
>
> 3. The faster hierarchies are different
> for *int* and *char* data structures (Why?)
>
> 4. Access to basic_string<unsigned int>
> takes a lot of time (Why?)
>
> Thanks,
> Alex
>
>P.S. Here are the results of the experiment.

[snip]

No offence, but what relevance does this have to people learning C++,
and hence to this newsgroup?

Your test, on your platform, using your nonstandard functions provided
those particular results, but who's to say whether (a) they are valid
or (b) they will be the same on any other platform? As far as I am
aware the only performance guidelines given by the C++ standard are
things like access times to STL objects, and those are relative
complexities.

In any case, a program which reports the same timing as 83, 103, 106,
108, 135 nsec (nanoseconds?) in five different cases must surely be
suspect. As must a supplied source code which doesn't include
iostream but does output using cout<<...

Regards,
Chris
[Cross-post removed]

--
Please reply to the newsgroup on which this text is posted.
If a private reply is appropriate, remove "spamfree." from my address.


0 new messages