July 11, 2021

Dictionary in C++ and in Python in comparison

The C++ version is faster, of course.

map.py
#########
d={
  "A":{"one":0, "two":0, "three": 0,},
  "B":{"one":0, "two":0, "three": 0,},
}
for i in range(20000000):
  d["A"]["two"]=5
  a=d["A"]["two"]
  #print(a)

map.cpp
###########
#include <iostream>
#include <map>
#include <string>
#include <string_view>
// g++ -O3 -std=c++2a map.cpp
 
int main()
{
    std::map<std::string, std::map<std::string, int>> d;
    d["A"]["one"]=0;
    d["A"]["two"]=0;
    d["A"]["three"]=0;
    d["B"]["one"]=0;
    d["B"]["two"]=0;
    d["B"]["three"]=0;

    for (int i=0;i<20000000;i++) {
      d["A"]["two"]=5;
      int a=d["A"]["two"];
      //std::cout<<a<<"\n";
    }
}

No comments:

Post a Comment