jeudi 23 avril 2015

how to reduce lookup time in c++

I am reading a very large file from SSD (Solid State Device) which has integers stored in it.

int main () {
  string line;
  srand (time(NULL));

  set<int> vec;
  for(unsigned long int j=0; j<1342177280; ++j){
     i = rand() % 10 + 1; //although my code performs something complex, for simplicity I am taking random numbers.
     vec.insert(i);
  } 

  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    int sum=0;
    while ( getline (myfile,line) )
    {
      int i=atoi(line.c_str());
      if(vec.count(i))
            sum+=vec;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

Since vec is a very large set, so vec.count(i) is taking maximum time for me. Is there some way by which I may reduce the time of lookups (vec.count(i)) in my code. If yes, then can someone please guide me as to how can I achieve the same?

I am using gcc version 4.8

Aucun commentaire:

Enregistrer un commentaire