I
Habe nochmal versucht mit memory mapped files die Sache etwas zu beschleunigen.
hier der Code:
namespace io = boost::iostreams;
int main(int argc, char* argv[])
{
unsigned int startTime = 0;
unsigned int endTime = 0;
const char *delims = ";";
const char* fname = "5mio.txt";
FILE *fp = fopen( fname, "rb" );
fseek(fp, 0, SEEK_END);
long pos = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *bytes = (char *)malloc(pos);
fread(bytes, pos, 1, fp);
fseek(fp, 0, SEEK_SET);
int lines(0),cols(0);
int i(0);
for (; i<=pos; i++)
{
if(lines < 1 && bytes[i] == *delims)
cols++;
if(bytes[i] == '\n')
lines++;
}
free(bytes);
cols++;
io::mapped_file_source myMap("5mio.txt");
io::stream<io::mapped_file_source> input(myMap);
// Create the native array
char*** myArray=allocate_2d_array_of_chars(lines,cols);
std::ofstream datei("cout.txt");
std::string line;
char buff[BUFSIZ];
int imyline(0),imycol(0);
while(std::getline(input, line))
{
strcpy(buff,line.c_str());
while (buff[start] != '\0')
{
size_t end = strcspn(buff + start, delims);
char token[end];
char szString[end];
szString[end]='\0';
strncpy(token, buff + start, end);
token[end]='\0';
if(imycol== (cols-1))
{
int i2 = 0;
for (int i = 0; i < end; i++)
{
char c = token[i];
if (c != '\r' && c != '\n')
{
szString[i2++] = c;
szString[i2]='\0';
}
}
myArray[imyline][imycol]=_strdup(szString);
}
else
{
myArray[imyline][imycol]=_strdup(token);
}
start += (buff[start + end] != '\0') ? end + 1 : end;
imycol++;
}
start = 0;
imyline++;
imycol=0;
}
for(int i = 0; i < lines; ++i)
{
for(int j = 0; j < cols; ++j)
{
(j==0)?(datei << myArray[i][j]):(datei << delims << myArray[i][j]);
}
datei << "\n";
}
return 0;
}
char*** allocate_2d_array_of_chars(size_t rows, size_t columns)
{
int i;
char ***db_array;
db_array = (char ***)malloc ( rows * sizeof *db_array);
if ( db_array == NULL )
{
puts ("Speicherreservierung nicht möglich");
return NULL;
}
for ( i = 0; i<rows; i++)
{
db_array[i] = (char **)malloc ( columns * sizeof *db_array[i]);
if ( db_array[i] == NULL )
printf ("Speicherreservierung bei db_array[%d] nicht möglich\n", i);
}
return db_array;
}
Die Sache ist das Dingen mit Mapping ist langsamer, ca. 3sek.
komisch oder....
P.S.: Bin für jede Verbesserung offen und dankbar...