Click here to Skip to main content
15,898,134 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionC++ Pin
Member 1461181419-May-20 5:46
Member 1461181419-May-20 5:46 
AnswerRe: C++ Pin
Greg Utas19-May-20 6:08
professionalGreg Utas19-May-20 6:08 
GeneralRe: C++ Pin
Richard MacCutchan19-May-20 6:13
mveRichard MacCutchan19-May-20 6:13 
AnswerRe: C++ Pin
CPallini19-May-20 20:50
mveCPallini19-May-20 20:50 
GeneralRe: C++ Pin
charlieg20-May-20 2:13
charlieg20-May-20 2:13 
GeneralRe: C++ Pin
CPallini20-May-20 2:16
mveCPallini20-May-20 2:16 
GeneralRe: C++ Pin
charlieg20-May-20 2:23
charlieg20-May-20 2:23 
GeneralRe: C++ Pin
CPallini20-May-20 3:48
mveCPallini20-May-20 3:48 
My little experiment gives me opposite results:
C++
#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 4:46
charlieg20-May-20 4:46 
GeneralRe: C++ Pin
CPallini20-May-20 4:59
mveCPallini20-May-20 4:59 
GeneralRe: C++ Pin
charlieg20-May-20 6:35
charlieg20-May-20 6:35 
QuestionC++ Pin
Member 1461181419-May-20 4:18
Member 1461181419-May-20 4:18 
AnswerRe: C++ Pin
Richard MacCutchan19-May-20 4:20
mveRichard MacCutchan19-May-20 4:20 
GeneralRe: C++ Pin
k505419-May-20 4:45
mvek505419-May-20 4:45 
GeneralRe: C++ Pin
Richard MacCutchan19-May-20 5:01
mveRichard MacCutchan19-May-20 5:01 
GeneralRe: C++ Pin
Member 1461181419-May-20 5:36
Member 1461181419-May-20 5:36 
GeneralRe: C++ Pin
Richard MacCutchan19-May-20 6:12
mveRichard MacCutchan19-May-20 6:12 
GeneralRe: C++ Pin
markkuk19-May-20 19:24
markkuk19-May-20 19:24 
QuestionOld guy question - VS2008 Professional and it's help system - why is it so f'd up? Pin
charlieg18-May-20 13:05
charlieg18-May-20 13:05 
AnswerRe: Old guy question - VS2008 Professional and it's help system - why is it so f'd up? Pin
Victor Nijegorodov18-May-20 21:05
Victor Nijegorodov18-May-20 21:05 
GeneralRe: Old guy question - VS2008 Professional and it's help system - why is it so f'd up? Pin
charlieg20-May-20 2:24
charlieg20-May-20 2:24 
QuestionC++ DB : SQLServer : SQLExecute - Insert statement returning Error -2 Pin
SureshBL18-May-20 12:00
SureshBL18-May-20 12:00 
AnswerRe: C++ DB : SQLServer : SQLExecute - Insert statement returning Error -2 Pin
Victor Nijegorodov18-May-20 21:01
Victor Nijegorodov18-May-20 21:01 
GeneralRe: C++ DB : SQLServer : SQLExecute - Insert statement returning Error -2 Pin
SureshBL19-May-20 3:02
SureshBL19-May-20 3:02 
GeneralRe: C++ DB : SQLServer : SQLExecute - Insert statement returning Error -2 Pin
Victor Nijegorodov19-May-20 7:36
Victor Nijegorodov19-May-20 7: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.