I have a program that reads data from a .csv and creates a multimap
. I've pasted a simple version below. It works fine, but runs really slowly on exit (i.e., once the program prints out the output and gets to return 0;
. I'm working relatively big files, e.g., anywhere from 50 to a few hundred megabytes. The delay on exit is, unsurprisingly, proportional to the file size.
I'm wondering if there is any way to speed up the exit? My guess is that it's slow because it's clearing data from memory. CTRL+c
in a terminal window shuts it down immediately, though. I've looked but haven't found much.
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
int main (int argc, char* argv[])
{
std::string filename = "edge_data_mid.csv";
// obtain multimaps
std::ifstream fin;
fin.open(filename.c_str(), std::ios_base::in|std::ios_base::binary);
if (!fin.is_open()) {
std::cout << "could not open the file '" << filename << "'" << std::endl;
exit(EXIT_FAILURE);
}
std::multimap<std::string, std::string> gciting;
std::string line;
// this loop parses the file one line at a time
while (getline(fin, line)) {
std::istringstream linestream(line);
std::string item;
std::string nodea;
std::string nodeb;
getline(linestream, item, ',');
nodea = item;
getline(linestream, item);
nodeb = item;
gciting.insert(std::pair<std::string, std::string>(nodeb, nodea));
}
fin.close();
std::cout << gciting.count("3800035") << std::endl;
return 0;
}