Click here to Skip to main content
14,692,140 members
Home » Discussions » C / C++ / MFC
   

C / C++ / MFC

 
QuestionC++ Pin
Member 1461181419-May-20 6:46
MemberMember 1461181419-May-20 6:46 
AnswerRe: C++ Pin
Greg Utas19-May-20 7:08
professionalGreg Utas19-May-20 7:08 
GeneralRe: C++ Pin
Richard MacCutchan19-May-20 7:13
mveRichard MacCutchan19-May-20 7:13 
AnswerRe: C++ Pin
CPallini19-May-20 21:50
mveCPallini19-May-20 21:50 
GeneralRe: C++ Pin
charlieg20-May-20 3:13
Membercharlieg20-May-20 3:13 
GeneralRe: C++ Pin
CPallini20-May-20 3:16
mveCPallini20-May-20 3:16 
GeneralRe: C++ Pin
charlieg20-May-20 3:23
Membercharlieg20-May-20 3:23 
GeneralRe: C++ Pin
CPallini20-May-20 4:48
mveCPallini20-May-20 4:48 
My little experiment gives me opposite results:
#include <iostream>
#include <map>
using namespace std;

constexpr int Indices = 4096;
constexpr int Size = 512;


extern  const int idx[Indices];
extern  const string tag [Size];



int linear_search(const string & s )
{ 
  for (int n = 0; n<Size; ++n)
    if ( s == tag[n]) return n;
  
  return -1;
}


constexpr int Iterations = 1000000;

int main()
{
  std::map<string, int> mtag;
  for (int n=0; n<Size; ++n)
  {
    mtag.insert(make_pair(tag[n], n));
  }

  int sum = 0;

  for (int n=0; n<Iterations; ++n)
  {
    int i = idx[n % Indices];
    const string & s  = tag[i];
    int k;
#ifdef LINEAR_SEARCH
    k = linear_search( s );
#else
    auto it = mtag.find(s);
    k = it->second;
#endif
    sum+=k;
  }

  cout << "sum " << sum << endl;
}
Where
  • tag is an array of 512 randomly generated strings (having length betwween 4 and 12)
  • idx is an array of 4096 randomly generated indices (for quickly gatering a candidate)


Output

g++ -D LINEAR_SEARCH -Wall lookup.cpp -o lookup_linear_search
g++  -Wall lookup.cpp -o lookup_map

time ./lookup_linear_search 
sum 252632422

real    0m1,821s
user    0m1,811s
sys     0m0,008s

time ./lookup_map
sum 252632422

real    0m0,297s
user    0m0,297s
sys     0m0,000s

GeneralRe: C++ Pin
charlieg20-May-20 5:46
Membercharlieg20-May-20 5:46 
GeneralRe: C++ Pin
CPallini20-May-20 5:59
mveCPallini20-May-20 5:59 
GeneralRe: C++ Pin
charlieg20-May-20 7:35
Membercharlieg20-May-20 7:35 
QuestionC++ Pin
Member 1461181419-May-20 5:18
MemberMember 1461181419-May-20 5:18 
AnswerRe: C++ Pin
Richard MacCutchan19-May-20 5:20
mveRichard MacCutchan19-May-20 5:20 
GeneralRe: C++ Pin
k505419-May-20 5:45
mvek505419-May-20 5:45 
GeneralRe: C++ Pin
Richard MacCutchan19-May-20 6:01
mveRichard MacCutchan19-May-20 6:01 
GeneralRe: C++ Pin
Member 1461181419-May-20 6:36
MemberMember 1461181419-May-20 6:36 
GeneralRe: C++ Pin
Richard MacCutchan19-May-20 7:12
mveRichard MacCutchan19-May-20 7:12 
GeneralRe: C++ Pin
markkuk19-May-20 20:24
Membermarkkuk19-May-20 20:24 
QuestionOld guy question - VS2008 Professional and it's help system - why is it so f'd up? Pin
charlieg18-May-20 14:05
Membercharlieg18-May-20 14:05 
AnswerRe: Old guy question - VS2008 Professional and it's help system - why is it so f'd up? Pin
Victor Nijegorodov18-May-20 22:05
MemberVictor Nijegorodov18-May-20 22:05 
GeneralRe: Old guy question - VS2008 Professional and it's help system - why is it so f'd up? Pin
charlieg20-May-20 3:24
Membercharlieg20-May-20 3:24 
QuestionC++ DB : SQLServer : SQLExecute - Insert statement returning Error -2 Pin
SureshBL18-May-20 13:00
MemberSureshBL18-May-20 13:00 
AnswerRe: C++ DB : SQLServer : SQLExecute - Insert statement returning Error -2 Pin
Victor Nijegorodov18-May-20 22:01
MemberVictor Nijegorodov18-May-20 22:01 
GeneralRe: C++ DB : SQLServer : SQLExecute - Insert statement returning Error -2 Pin
SureshBL19-May-20 4:02
MemberSureshBL19-May-20 4:02 
GeneralRe: C++ DB : SQLServer : SQLExecute - Insert statement returning Error -2 Pin
Victor Nijegorodov19-May-20 8:36
MemberVictor Nijegorodov19-May-20 8:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.