mardi 31 décembre 2019

How to improve vector insert time

I have CSV file. Which contain the 1086098 lines of logs. I have written c++ code. where i search for some text and based on founding i report the issues to the respective owner.

Implementation Details

  1. Load all the file logs to std::vector.

  2. Search in the vector at run time.

Problem : push_back in the std::vector takes 1683087 ms time. How can i improve on this time. Any other suggestion is welcome.

std::vector<std::string> complete_log;

bool LoadFileToVector(const std::string& str) {
  std::string needle = str;
  std::ifstream fin(needle.c_str());
  std::string line;
  bool found = false;

  if (fin.is_open()) {
    if (!is_empty(fin)) {
      fin.exceptions(std::ifstream::badbit);

      try {
        while (getline(fin, line)) {
          complete_log.push_back(line);

        }

        for (const auto& text : start_check) {

          found = false;
          for (auto elem : complete_log) {
            if (elem.find(text) != std::string::npos) {
              basic_result[text] = true;
              found = true;
            }
          }
          if (!found)
            basic_result[text] = false;
        }

      } catch (std::ifstream::failure& FileExcep) {
        std::cout << "Caught an exception = " << FileExcep.what() << std::endl;
        fin.close();
        return false;
      } catch (...) {
        std::cout << "Unkown Exception/n";
      }
    } else {
      std::cout << "Input file "<<needle<<" is Empty" << std::endl;
      fin.close();
      return false;
    }

    fin.close();
    return true;
  } else {
    std::cout << "Cannot open file to update map" << std::endl;
    return false;
  }
  return true;
}

Aucun commentaire:

Enregistrer un commentaire