#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include <unordered_map>
#include <sys/time.h>
using namespace std;
class StopWatch {
public :
StopWatch( string title)
: title_( title) {
gettimeofday( & s_, 0 ) ;
}
~StopWatch( ) {
gettimeofday( & e_, 0 ) ;
printf ( "%s time = %lf\n " , title_.c_str ( ) , ( e_.tv_sec - s_.tv_sec ) + ( e_.tv_usec - s_.tv_usec ) * 1.0E-6 ) ;
}
private :
struct timeval s_, e_;
string title_;
} ;
const int COUNT = 100000 ;
int main( ) {
// your code goes here
{
StopWatch sw( "vector" ) ;
std:: vector < int > v;
for ( int i = 0 ; i < COUNT; i++ ) {
v.push_back ( i) ;
}
}
{
StopWatch sw( "reserved vector" ) ;
std:: vector < int > v;
v.reserve ( COUNT) ;
for ( int i = 0 ; i < COUNT; i++ ) {
v.push_back ( i) ;
}
}
std:: map < std:: string , int > m;
{
StopWatch sw( "map set" ) ;
for ( int i = 0 ; i < COUNT; i++ ) {
std:: stringstream ss;
ss << i << ":" ;
m[ ss.str ( ) ] = i;
}
}
std:: unordered_map < std:: string , int > um;
{
StopWatch sw( "unordered map set" ) ;
for ( int i = 0 ; i < COUNT; i++ ) {
std:: stringstream ss;
ss << i << ":" ;
um[ ss.str ( ) ] = i;
}
}
{
StopWatch sw( "map get" ) ;
int total = 0 ;
for ( int i = 0 ; i < COUNT; i++ ) {
std:: stringstream ss;
ss << i << ":" ;
total + = m[ ss.str ( ) ] ;
}
}
{
StopWatch sw( "unordered map get" ) ;
int total = 0 ;
for ( int i = 0 ; i < COUNT; i++ ) {
std:: stringstream ss;
ss << i << ":" ;
total + = um[ ss.str ( ) ] ;
}
}
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3N0cmVhbT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPHVub3JkZXJlZF9tYXA+CiNpbmNsdWRlID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