Re: map.insert(key,val) vs. map[key]=val ?

From: Patrick Guio (patrickg_at_ii.uib.no)
Date: 10/19/04


Date: Tue, 19 Oct 2004 18:27:55 +0200

On Tue, 19 Oct 2004, Victor Bazarov wrote:

>> I have benchmarked 2 different ways to add an element in a map container.
>>
>> 1 map.insert(key,val)
>> 2 map[key]=val
>>
>> I used gcc and it comes out that the second way is about twice as fast as
>> the first one.
>> Is there a reason/case why I should use the insert() method rather the
>> operator[]?
>> Are the 2 different completely equivalent?
>
> No, they are not. According to the Standard, the second one creates
> a default 'value' associated with 'key' in the container, and then uses
> the assignment op to get the contents of 'val' into the newly created
> value. The operator[] is said to be implemented in terms of 'insert'.
> So, all things being standard, I'd expect 'insert' to work faster in
> general.

Strange, here is my very simple (maybe too simple?) benchmark

#include <map>
#include <string>
#include <iostream>
#include <ctime>

typedef std::map<int, std::string> lut;
typedef lut::value_type pair;

#define BENCH(BLOCK,NAME,N) \
{ \
   lut a; \
   clock_t tic=clock(); \
   for (int i=0; i<N; i++) { \
     BLOCK \
   } \
   double elapsed = double(clock()-tic)/N/CLOCKS_PER_SEC; \
   std::cout << "Time elapsed = " << 1e9*elapsed << " ns" << std::endl; \
} \

int const N=100000;

int main()
{
   BENCH(a.insert(pair(i%1000,"foo1"));,"map::insert()",N)
   BENCH(a[i%1000]="foo1";,"map::operator[]",N)

   BENCH(lut a; a.insert(pair(i%1000,"foo1"));,"map::insert()",N)
   BENCH(lut a; a[i%1000]="foo1";,"map::operator[]",N)
}

And the output on a linux box runnig g++ 3.2.3 compiling without any
optimisation

Time elapsed = 900 ns
Time elapsed = 500 ns
Time elapsed = 1100 ns
Time elapsed = 1200 ns

How do you explain this?

Sincerely,

P



Relevant Pages

  • Re: Requesting advice how to clean up C code for validating string represents integer
    ... GNU C is the C-like language accepted by gcc. ... violate the C standard. ... that you get the GNU C version of each library rather than the ANSI ...
    (comp.lang.c)
  • Re: [OT] lcc first experience
    ... GCC does embrace-and-extend? ... affect the behaviour of a strictly conforming program. ... Of course if implementations are called "lcc-win" you will start ranting ... making them 100% compatible with the C standard. ...
    (comp.lang.c)
  • Re: KEil bought by ARM
    ... >> against and that a user of gcc can add tests to. ... > tests demonstrate conformance to a recognized C standard like C89 ... and possibly with some extensions. ... > were renumbered and became clauses in the ISO standard. ...
    (comp.arch.embedded)
  • Re: non-standard functions in libc -- bad design?
    ... standard supported by a compiler/library, ... I'd call them extensions. ... gcc doesn't get a vote. ... gcc is just a compiler, ...
    (comp.lang.c)
  • Re: Variable declaration - Is this valid in standard C?
    ... YES it is valid in standard C!!!! ... doesn't have a conforming C90 compiler (ignoring minor bugs, ... are in C99 but not in C90. ... That's what the gcc project's own web site says, ...
    (comp.lang.c)