Mail Archives: djgpp/2002/03/12/05:17:15
Performance of for-loop and for_each algorithm
in vector, string, list, set
was measured when using C/C++ Program Perfometer at
http://groups.google.com/groups?selm=3C84CF9B.AA99EFA8%40bigfoot.com
===============================================================
gpp : GNU C++ version 2.95.3 20010315/djgpp (release) (djgpp)
compiled by GNU C version 2.95.3 20010315/djgpp (release).
---------
Windows98
===============================================================
--- No optimization ---
#================================================
# Comparison Group#1 of 1 : for-loop vs. for_each
#------------------------------------------------
# Resource Name : user time used
# Resource Cost Unit : uclock
# Resource State Unit : uclock
#================================================
: -------------------------------------------------------------------------
: f3 : demo3.c #68 : for-loop - vector size 100 -> 60221
: f3 : demo3.c #88 : for-loop - string size 100 -> 554565
: f3 : demo3.c #109 : for-loop - list size 100 -> 348958
: f3 : demo3.c #130 : for-loop - set size 100 -> 468564
: f3 : demo3.c #151 : for_each algorithm - vector size 100 -> 38634
: f3 : demo3.c #169 : for_each algorithm - string size 100 -> 47679
: f3 : demo3.c #187 : for_each algorithm - list size 100 -> 133318
: f3 : demo3.c #205 : for_each algorithm - set size 100 -> 310829
: -------------------------------------------------------------------------
--- Optimization O1 ---
#================================================
# Comparison Group#1 of 1 : for-loop vs. for_each
#------------------------------------------------
# Resource Name : user time used
# Resource Cost Unit : uclock
# Resource State Unit : uclock
#================================================
: -------------------------------------------------------------------------
: f3 : demo3.c #68 : for-loop - vector size 100 -> 27802
: f3 : demo3.c #88 : for-loop - string size 100 -> 55053
: f3 : demo3.c #109 : for-loop - list size 100 -> 42867
: f3 : demo3.c #130 : for-loop - set size 100 -> 67939
: f3 : demo3.c #151 : for_each algorithm - vector size 100 -> 31306
: f3 : demo3.c #169 : for_each algorithm - string size 100 -> 32563
: f3 : demo3.c #187 : for_each algorithm - list size 100 -> 36218
: f3 : demo3.c #205 : for_each algorithm - set size 100 -> 68370
: -------------------------------------------------------------------------
--- Optimization O2 ---
#================================================
# Comparison Group#1 of 1 : for-loop vs. for_each
#------------------------------------------------
# Resource Name : user time used
# Resource Cost Unit : uclock
# Resource State Unit : uclock
#================================================
: -------------------------------------------------------------------------
: f3 : demo3.c #68 : for-loop - vector size 100 -> 29404
: f3 : demo3.c #88 : for-loop - string size 100 -> 55141
: f3 : demo3.c #109 : for-loop - list size 100 -> 38100
: f3 : demo3.c #130 : for-loop - set size 100 -> 70248
: f3 : demo3.c #151 : for_each algorithm - vector size 100 -> 32006
: f3 : demo3.c #169 : for_each algorithm - string size 100 -> 32522
: f3 : demo3.c #187 : for_each algorithm - list size 100 -> 36340
: f3 : demo3.c #205 : for_each algorithm - set size 100 -> 69047
: -------------------------------------------------------------------------
--- Optimization O3 ---
#================================================
# Comparison Group#1 of 1 : for-loop vs. for_each
#------------------------------------------------
# Resource Name : user time used
# Resource Cost Unit : uclock
# Resource State Unit : uclock
#================================================
: -------------------------------------------------------------------------
: f3 : demo3.c #68 : for-loop - vector size 100 -> 4911
: f3 : demo3.c #88 : for-loop - string size 100 -> 31126
: f3 : demo3.c #109 : for-loop - list size 100 -> 16558
: f3 : demo3.c #130 : for-loop - set size 100 -> 45196
: f3 : demo3.c #151 : for_each algorithm - vector size 100 -> 29931
: f3 : demo3.c #169 : for_each algorithm - string size 100 -> 30789
: f3 : demo3.c #187 : for_each algorithm - list size 100 -> 33768
: f3 : demo3.c #205 : for_each algorithm - set size 100 -> 64397
: -------------------------------------------------------------------------
// =================== File demo.h : BEGIN ====================
#ifndef _DEMO_H
#define _DEMO_H
//---------------------------
#include "adapt.h"
//---------------------------
#define TURN_ON_uclock(x) TURN_ON (uclock_t, uclock_t, RESOURCE_user_time_used, x)
//---------------------------
void f1 (void);
void f2 (void);
void f3 (void);
#endif
// =================== File demo.h : END ======================
// =================== File demo.c : BEGIN ====================
#include "demo.h"
void SetEnvIt ()
{
SetTotalTests (25);
SetScaleAndTotalIterations (10000, 100000);
}
void MeasureIt ()
{
}
void CompareIt ()
{
CompareFunc ("for-loop vs. for_each", f3);
}
// =================== File demo.c : END ======================
// =================== File demo3.c : BEGIN ====================
#include "demo.h"
//---------------------------
#define SIZE_OF_CONTAINER 100
typedef unsigned int uint;
// ==========================
void foo (int val_i)
{
val_i * 2;
}
// ==========================
void f3 (void)
{
// ------ Preparation ------
const string text_for_loop ("for-loop");
const string text_for_each ("for_each algorithm");
size_t text_find_setw = MAX_VALUE (text_for_loop.size(), text_for_each.size());
size_t text_container_name_setw = 6;
ostringstream osstr;
osstr.setf (ios::left, ios::adjustfield);
vector<uint> uint_vector;
vector<uint>::iterator iter_vector;
basic_string<uint> uint_string;
basic_string<uint>::iterator iter_string;
list<uint> uint_list;
list<uint>::iterator iter_list;
set<uint> uint_set;
set<uint>::iterator iter_set;
for (uint i = 0; i < SIZE_OF_CONTAINER; i++)
{
uint_vector.push_back(i);
uint_string += i;
uint_list.push_back(i);
uint_set.insert(i);
}
assert (uint_vector.size() == SIZE_OF_CONTAINER);
assert (uint_string.size() == SIZE_OF_CONTAINER);
assert (uint_list.size() == SIZE_OF_CONTAINER);
assert (uint_set.size() == SIZE_OF_CONTAINER);
// -------- for-loop : uint_vector --------
{
osstr.str(string());
osstr << setw (text_find_setw)
<< text_for_loop.c_str()
<< " - "
<< setw(text_container_name_setw)
<< "vector"
<< " size "
<< uint_vector.size();
TURN_ON_uclock(osstr.str())
{
for (iter_vector = uint_vector.begin(); iter_vector != uint_vector.end();
iter_vector++)
{
foo (*iter_vector);
}
}
}
// -------- for-loop : uint_string --------
{
osstr.str(string());
osstr << setw (text_find_setw)
<< text_for_loop.c_str()
<< " - "
<< setw(text_container_name_setw)
<< "string"
<< " size "
<< uint_string.size();
TURN_ON_uclock(osstr.str())
{
for (iter_string = uint_string.begin(); iter_string != uint_string.end();
iter_string++)
{
foo (*iter_string);
}
}
}
// -------- for-loop : uint_list --------
{
osstr.str(string());
osstr << setw (text_find_setw)
<< text_for_loop.c_str()
<< " - "
<< setw(text_container_name_setw)
<< "list"
<< " size "
<< uint_list.size();
TURN_ON_uclock(osstr.str())
{
for (iter_list = uint_list.begin(); iter_list != uint_list.end(); iter_list++)
{
foo (*iter_list);
}
}
}
// -------- for-loop : uint_set --------
{
osstr.str(string());
osstr << setw (text_find_setw)
<< text_for_loop.c_str()
<< " - "
<< setw(text_container_name_setw)
<< "set"
<< " size "
<< uint_set.size();
TURN_ON_uclock(osstr.str())
{
for (iter_set = uint_set.begin(); iter_set != uint_set.end(); iter_set++)
{
foo (*iter_set);
}
}
}
// -------- for_each : uint_vector --------
{
osstr.str(string());
osstr << setw (text_find_setw)
<< text_for_each.c_str()
<< " - "
<< setw(text_container_name_setw)
<< "vector"
<< " size "
<< uint_vector.size();
TURN_ON_uclock(osstr.str())
{
for_each (uint_vector.begin(), uint_vector.end(), foo);
}
}
// -------- for_each : uint_string --------
{
osstr.str(string());
osstr << setw (text_find_setw)
<< text_for_each.c_str()
<< " - "
<< setw(text_container_name_setw)
<< "string"
<< " size "
<< uint_string.size();
TURN_ON_uclock(osstr.str())
{
for_each (uint_string.begin(), uint_string.end(), foo);
}
}
// -------- for_each : uint_list --------
{
osstr.str(string());
osstr << setw (text_find_setw)
<< text_for_each.c_str()
<< " - "
<< setw(text_container_name_setw)
<< "list"
<< " size "
<< uint_list.size();
TURN_ON_uclock(osstr.str())
{
for_each (uint_list.begin(), uint_list.end(), foo);
}
}
// -------- for_each : uint_set --------
{
osstr.str(string());
osstr << setw (text_find_setw)
<< text_for_each.c_str()
<< " - "
<< setw(text_container_name_setw)
<< "set"
<< " size "
<< uint_set.size();
TURN_ON_uclock(osstr.str())
{
for_each (uint_set.begin(), uint_set.end(), foo);
}
}
} // f3
// =================== File demo3.c : END ======================
===========================
Alex Vinokur
mailto:alexvn AT bigfoot DOT com
mailto:alexvn AT go DOT to
http://up.to/alexvn
http://go.to/alexv_math
===========================
- Raw text -