I suspect that accessing the same map from multiple threads will not improve the performance.
Printable View
I suspect that accessing the same map from multiple threads will not improve the performance.
If you are able to change the creation of the input file ....
use unformatted I/O rather than text I/O. That should give
a significant performance improvment.
In order to archive something like above, I have to mix up a lot of if and else if when reading each of the map array. Here is the final codeIt looks like a real mess inside the for loop, but I know nothing other than if/else if to do that. Any comment to shorten it or to make it look nicer are welcome.Code:#include <map>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
struct Point {
string Name;
int xPos;
Point(const string A,const int B) :
Name(A),xPos(B) {}
};
struct PointCmp {
bool operator()( const Point & b1, const Point & b2 ) {
if ( b1.Name < b2.Name || ( b1.Name == b2.Name && b1.xPos < b2.xPos ) ) {
return true;
} else {
return false;
}
}
};
typedef map<Point, int, PointCmp> mapPoint;
int main (int argc, char * const argv[]) {
if ( argc == 1 ) {
fprintf(stderr, "Usage: xyTest <input> <output>\n");
return 1;
}
mapPoint mPoint;
ifstream fin(argv[1]);
string tempName;
int x1, x2;
while ( fin >> tempName >> x1 >> x2 ) {
string temp;
getline(fin,temp);
for ( int i = x1; i <= x2; ++i ) {
Point A(tempName,i);
++mPoint[A];
}
}
fin.close();
int tempCount;
int tempPos;
ofstream fout(argv[2]);
for ( mapPoint::iterator i = mPoint.begin(), end = mPoint.end(); i != end; ++i ) {
if ( i == mPoint.begin() ) {
tempCount = i->second;
tempName = (i->first).Name;
tempPos = (i->first).xPos;
fout << tempName << '\t' << 0 << '\t' << tempPos - 1 << '\t' << 0 << '\n';
fout << tempName << '\t' << tempPos << '\t';
} else if ( i->second == tempCount && tempName == (i->first).Name && (i->first).xPos == tempPos + 1 ) {
++tempPos;
if ( i == --mPoint.end() ) {
fout << tempPos << '\t' << tempCount << '\n';
}
} else if ( i->second == tempCount && (tempName == (i->first).Name) ) {
fout << tempPos << '\t' << tempCount << '\n';
fout << tempName << '\t' << tempPos + 1 << '\t' << (i->first).xPos - 1 << '\t' << 0 << '\n';
tempPos = (i->first).xPos;
fout << tempName << '\t' << tempPos << '\t';
} else if ( tempName == (i->first).Name && ((i->first).xPos == tempPos + 1) ) {
fout << tempPos << '\t' << tempCount << '\n';
++tempPos;
fout << tempName << '\t' << (i->first).xPos << '\t';
tempCount = i->second;
} else if ( tempName == (i->first).Name ) {
fout << tempPos << '\t' << tempCount << '\n';
fout << tempName << '\t' << tempPos + 1 << '\t' << (i->first).xPos - 1 << '\t' << 0 << '\n';
fout << tempName << (i->first).xPos << '\t';
} else {
fout << tempPos << '\t' << tempCount << '\n';
tempCount = i->second;
tempName = (i->first).Name;
tempPos = (i->first).xPos;
fout << tempName << '\t' << 0 << '\t' << tempPos - 1 << '\t' << 0 << '\n';
fout << tempName << '\t' << tempPos << '\t';
}
}
fout.close();
return 0;
}