Click here to Skip to main content
15,909,242 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Problem with pointers memory allocation on the same memory area Pin
Member 959508024-May-20 22:40
Member 959508024-May-20 22:40 
GeneralRe: Problem with pointers memory allocation on the same memory area Pin
Richard MacCutchan24-May-20 22:54
mveRichard MacCutchan24-May-20 22:54 
GeneralRe: Problem with pointers memory allocation on the same memory area Pin
Member 959508024-May-20 23:03
Member 959508024-May-20 23:03 
GeneralRe: Problem with pointers memory allocation on the same memory area Pin
Richard MacCutchan24-May-20 23:19
mveRichard MacCutchan24-May-20 23:19 
GeneralRe: Problem with pointers memory allocation on the same memory area Pin
Member 95950801-Jun-20 20:36
Member 95950801-Jun-20 20:36 
AnswerRe: Problem with pointers memory allocation on the same memory area Pin
phil.o25-May-20 0:21
professionalphil.o25-May-20 0:21 
GeneralRe: Problem with pointers memory allocation on the same memory area Pin
Richard MacCutchan25-May-20 0:31
mveRichard MacCutchan25-May-20 0:31 
GeneralRe: Problem with pointers memory allocation on the same memory area Pin
Member 959508025-May-20 1:25
Member 959508025-May-20 1:25 
GeneralRe: Problem with pointers memory allocation on the same memory area Pin
phil.o25-May-20 1:36
professionalphil.o25-May-20 1:36 
GeneralRe: Problem with pointers memory allocation on the same memory area Pin
Richard MacCutchan25-May-20 1:46
mveRichard MacCutchan25-May-20 1:46 
AnswerRe: Problem with pointers memory allocation on the same memory area Pin
Member 95950801-Jun-20 20:42
Member 95950801-Jun-20 20:42 
QuestionPrinting With Crystal Report Pin
a_alise21-May-20 11:09
a_alise21-May-20 11:09 
AnswerRe: Printing With Crystal Report Pin
_Flaviu22-May-20 0:12
_Flaviu22-May-20 0:12 
QuestionDynamic Created CEdit using CreateEx Pin
Beginner_mfc20-May-20 3:48
Beginner_mfc20-May-20 3:48 
AnswerRe: Dynamic Created CEdit using CreateEx Pin
Richard MacCutchan20-May-20 5:06
mveRichard MacCutchan20-May-20 5:06 
AnswerRe: Dynamic Created CEdit using CreateEx Pin
Victor Nijegorodov20-May-20 6:42
Victor Nijegorodov20-May-20 6:42 
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 

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.