I have the following C++ program:
#include <map>
#include <string>
#include <QDebug>
#include <QMap>
#include <QElapsedTimer>
using namespace std;
int main()
{
QElapsedTimer t;
qint64 e;
const int COUNT = 1000000;
// using std::map
t.start();
map<int,string> m;
for(int i = 0; i < COUNT; i++)
m[i] = "HI";
e = t.elapsed();
qDebug() << e;
// using Map
t.restart();
QMap<int,string> m2;
for(int i = 0; i < COUNT; i++)
m2[i] = "HI";
e = t.elapsed();
qDebug() << e;
return 0;
}
#include <map>
#include <string>
#include <QDebug>
#include <QMap>
#include <QElapsedTimer>
using namespace std;
int main()
{
QElapsedTimer t;
qint64 e;
const int COUNT = 1000000;
// using std::map
t.start();
map<int,string> m;
for(int i = 0; i < COUNT; i++)
m[i] = "HI";
e = t.elapsed();
qDebug() << e;
// using Map
t.restart();
QMap<int,string> m2;
for(int i = 0; i < COUNT; i++)
m2[i] = "HI";
e = t.elapsed();
qDebug() << e;
return 0;
}
To copy to clipboard, switch view to plain text mode
Compiled in release mode, the std::map version gives me a result of 208 ms, while the QMap version gives me a result of 220 ms.
But the .Net version is much faster
using System;
using System.Collections.Generic;
using System.Diagnostics;
internal class Program
{
private static void Main()
{
Stopwatch w = Stopwatch.StartNew();
Dictionary<int, string> dic = new Dictionary<int, string>();
long e;
const int COUNT = 1000000;
for (int i = 0; i < COUNT; i++)
{
dic[i] = "HI";
}
w.Stop();
e = w.ElapsedMilliseconds;
Console.WriteLine(e);
}
}
using System;
using System.Collections.Generic;
using System.Diagnostics;
internal class Program
{
private static void Main()
{
Stopwatch w = Stopwatch.StartNew();
Dictionary<int, string> dic = new Dictionary<int, string>();
long e;
const int COUNT = 1000000;
for (int i = 0; i < COUNT; i++)
{
dic[i] = "HI";
}
w.Stop();
e = w.ElapsedMilliseconds;
Console.WriteLine(e);
}
}
To copy to clipboard, switch view to plain text mode
This gives me a result of 60 ms!
Am I doing something wrong? Or the result is actually normal?
Bookmarks