For some reason I split my code into 2 parts; first part is written in C and second part with python. I wrote the output of C code in file and use it in python as my input, now my problem is when I want to load the file into the numpy array in takes about 18 minutes which is a lot and I need to reduce this time. the size of fie is around 300MB.
The C code for writing into the file is like:
struct point {
float fpr;
float tpr;
point(float x, float y)
{
fpr = x;
tpr = y;
}
};
vector<point> current_points;
// filling current_points ......
ofstream files;
files.open ("./allpoints.txt")
for(unsigned int i=0; i<current_points.size(); i++)
files << current_points[i].fpr << '\t' << current_points[i].tpr << "\n";
And reading the file in python is like:
with open("./allpoints.txt") as f:
just_comb = numpy.loadtxt(f) #The problem is here (took 18 minutes)
The allpoints.txt
is like this (As you can see it's coordination of some points in 2D dimension):
0.989703 1
0 0
0.0102975 0
0.0102975 0
1 1
0.989703 1
1 1
0 0
0.0102975 0
0.989703 1
0.979405 1
0 0
0.020595 0
0.020595 0
1 1
0.979405 1
1 1
0 0
0.020595 0
0.979405 1
0.969108 1
...
...
...
0 0
0.0308924 0
0.0308924 0
1 1
0.969108 1
1 1
0 0
0.0308924 0
0.969108 1
0.95881 1
0 0
Now my question is that, is there any better way to store the vector of points in file (something like binary format ) and read it in python into 2D numpy array faster?