#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <ctime>
class SimpleObject
{
int _int;
float _float;
public :
SimpleObject( ) : _int( 0 ) , _float( 0.0f ) { }
int GetInt( ) { return _int; }
float GetFloat( ) { return _float; }
void incrementInt( ) { ++ _int; }
void incrementFloat( ) { _float + = 1.0f ; }
} ;
std:: size_t getCount( const std:: string & prompt)
{
std:: string Input;
std:: cout << prompt << "\n > " ;
std:: getline ( std:: cin , Input) ;
if ( Input == "exit" )
return 0 ;
return std:: stoi ( Input) ;
}
void doForLoop( std:: vector < SimpleObject> & v, std:: size_t iterations)
{
for ( std:: size_t i = 0 ; i < iterations; ++ i)
{
for ( auto it = v.begin ( ) ; it ! = v.end ( ) ; ++ it)
{
it- > incrementFloat( ) ;
it- > incrementInt( ) ;
}
}
}
void doForEach( std:: vector < SimpleObject> & v, std:: size_t iterations)
{
for ( std:: size_t i = 0 ; i < iterations; ++ i)
for_each( v.begin ( ) , v.end ( ) , [ ] ( SimpleObject& o) { o.incrementFloat ( ) ; o.incrementInt ( ) ; } ) ;
}
void doRangedFor( std:: vector < SimpleObject> & v, std:: size_t iterations)
{
for ( std:: size_t i = 0 ; i < iterations; ++ i)
for ( auto & e : v)
e.incrementFloat ( ) , e.incrementInt ( ) ;
}
using func_type = void ( * ) ( std:: vector < SimpleObject> & , std:: size_t ) ;
void test( const std:: string & testName, func_type testme, std:: size_t iterations, std:: size_t objects)
{
std:: vector < SimpleObject> v( objects) ;
clock_t begin = clock ( ) ;
testme( v, iterations) ;
clock_t end = clock ( ) ;
std:: cout << "Test \" " << testName << "\" took " << ( float ( end) - begin) / CLOCKS_PER_SEC << " seconds.\n " ;
}
int main( int argc, char ** argv)
{
std:: size_t nIterations;
std:: size_t nObjects;
do
{
nIterations = getCount( "Enter the number of iterations:" ) ;
nObjects = getCount( "Enter the number of objects in the container:" ) ;
if ( nIterations && nObjects)
{
std:: cout << "\n \n We will be performing " << nIterations << " iterations on a container holding " ;
std:: cout << nObjects << " objects.\n " ;
test( "for loop" , doForLoop, nIterations, nObjects) ;
test( "for each" , doForEach, nIterations, nObjects) ;
test( "ranged for" , doRangedFor, nIterations, nObjects) ;
}
} while ( nIterations && nObjects) ;
}
I2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3RpbWU+CgpjbGFzcyBTaW1wbGVPYmplY3QgCnsKICAgIGludCBfaW50OwogICAgZmxvYXQgX2Zsb2F0OwoKcHVibGljOgogICAgU2ltcGxlT2JqZWN0KCkgOiBfaW50KDApLCBfZmxvYXQoMC4wZikge30KCiAgICBpbnQgR2V0SW50KCkgeyByZXR1cm4gX2ludDsgfQogICAgZmxvYXQgR2V0RmxvYXQoKSB7IHJldHVybiBfZmxvYXQ7IH0KCiAgICB2b2lkIGluY3JlbWVudEludCgpIHsgKytfaW50OyB9CiAgICB2b2lkIGluY3JlbWVudEZsb2F0KCkgeyBfZmxvYXQgKz0gMS4wZjsgfQp9OwoKc3RkOjpzaXplX3QgZ2V0Q291bnQoY29uc3Qgc3RkOjpzdHJpbmcmIHByb21wdCkKewogICAgc3RkOjpzdHJpbmcgSW5wdXQ7CgogICAgc3RkOjpjb3V0IDw8IHByb21wdCA8PCAiXG4+ICI7CgogICAgc3RkOjpnZXRsaW5lKHN0ZDo6Y2luLCBJbnB1dCk7CgogICAgaWYgKElucHV0ID09ICJleGl0IikKICAgICAgICByZXR1cm4gMDsKCiAgICByZXR1cm4gc3RkOjpzdG9pKElucHV0KTsKfQoKdm9pZCBkb0Zvckxvb3Aoc3RkOjp2ZWN0b3I8U2ltcGxlT2JqZWN0PiYgdiwgc3RkOjpzaXplX3QgaXRlcmF0aW9ucykKewogICAgZm9yIChzdGQ6OnNpemVfdCBpID0gMDsgaSA8IGl0ZXJhdGlvbnM7ICsraSkKICAgIHsKICAgICAgICBmb3IgKGF1dG8gaXQgPSB2L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