Lecture d'un tableau contenu dans un fichier
Bonjour à tous,
J'ai en entrée de mon application un fichier contenant un tableau de valeurs (taille N*N pour le moment).
J'ai problème au niveau du temps d'exécution. avec N = 201, ma lecture de fichier prend 250ms.
Première méthode :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
std::ifstream file(_fileName.c_str());
if(!file)
return;
// find size of array
int arraySize = std::count( std::istreambuf_iterator<char>( file ), std::istreambuf_iterator<char>(), '\n' );
file.seekg(0, std::ios::beg);
std::vector< double > array( arraySize * arraySize );
std::string line;
int index = 0;
double tempVal;
while(std::getline(file, line))
{
std::istringstream iss(line);
//while( iss >> array[index] )
// ++index;
while( iss >> tempVal )
array.at(index++) = tempVal;
} |
Deuxième méthode :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
std::ifstream file(_fileName.c_str());
if(!file)
return;
// find size of array
int arraySize = std::count( std::istreambuf_iterator<char>( file ), std::istreambuf_iterator<char>(), '\n' );
file.seekg(0, std::ios::beg);
std::vector< double > array( arraySize * arraySize );
std::stringstream buffer;
buffer << file.rdbuf();
double temp;
int index = 0;
while(buffer >> array[index++]); |
Troisième méthode :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
std::ifstream file(_fileName.c_str());
if(!file)
return;
std::vector< double > array;
std::stringstream buffer;
buffer << file.rdbuf();
double temp;
while(buffer >> temp)
array.push_back(temp); |
Ces trois versions prennent le même temps.