There are two ways of map insertion:
m[key] = val;
Or
m.insert(make_pair(key, val));
My question is, which op
On a lightly loaded system with plenty of memory, this code:
#include #include #include #include using namespace std; typedef map MapType; const unsigned int NINSERTS = 1000000; int main() { MapType m1; string s = "foobar"; clock_t t = clock(); for ( unsigned int i = 0; i < NINSERTS; i++ ) { m1[i] = s; } cout << clock() - t << endl; MapType m2; t = clock(); for ( unsigned int i = 0; i < NINSERTS; i++ ) { m2.insert( make_pair( i, s ) ); } cout << clock() - t << endl; }
produces:
1547 1453
or similar values on repeated runs. So insert is (in this case) marginally faster.