In doing a performance comparison between several linear algebra libraries I had to read in several large (more than 21 million non zero values) sparse matrices. I’m not going to claim that this is the fastest way to read in a matrix that is stored on disk, but for me it was fast enough.

The Data Structure

This struct contains three std::vectors which store the row, column and value entries from each line in the file. Some assumptions are made on the matrix, namely that there are no rows will all zero entries and that the lass column with data is the last column in the matrix. If your matrix is larger than this then you will need to manually modify the data structure that you store your matrix into. The matlab ascii sparse matrix format does not store the number of rows and columns Reference.

Read a file as a string

Another important part of all of this is to read the sparse matrix into memory as fast as possible so that we can do something with it. This method was taken from this excellent comparison of different ways to read a file into a std::string.

Read into our COO structure

Once the file has been read into a string we can convert it into a std::stringstream and used it to convert ascii into values. Note that it might be slightly faster/memory efficient to return a stringstream from the ReadFileAsString function directly.