'How to approximate the size of a text file containing a matrix, written by Fortran?
I have written an N*N matrix to a formatted text file using Fortran. The Fortran output format was N(3X,E17.8), where N is the matrix size.
Example of one matrix element: 0.19860753E-08 i.e. "(three spaces)0.19860753E-08".
A 558x558 Matrix has a file size of 5.294.302 Bytes.
How could I approximate the file size for an N*N matrix for any value of N?
Solution 1:[1]
For a single record written by a format like
write (*, '(999(3X,E17.8)') a
we can determine how many characters would be written. For each element there will be three spaces followed by a field of width 17: a total of 20 characters for each element. There will then be a number of characters to end the record (depending on filesystem, operating system, etc., usually one or two).
Knowing how large each record is, and how many records are written, you have the size. Depending again on system settings, you may also see a separate end-of-file marker of some size.
We can answer this question because we know how large each field is going to be. We don't always know that from a format. For example, with some edit descriptors the field width is determined later on: A, I0, G0.6, etc., are such.
Finally, note also that without colon editing we may get extra output for an element. X is a special case, if we instead had
write (*, '(999(" ",E17.8)') a
then there would be three spaces additional spaces after the final element written out before the end of the record. This can be prevented in
write (*, '(999(" ":,,E17.8)') a
X is a position edit descriptor: it doesn't actually transfer data so doesn't add to the transfer count unless further data are written.
Solution 2:[2]
Rather than calculating the size of the file from the details of the code and the file format, it may be simpler to write out files containing matrices with (at least) three different values of N. You can then fit the file size S as a function of N, as
S = a*N^2 + b*N + c
where a, b and c are constants which you will get from the fit.
- The
a*N^2term comes from the representation of the numbers in the matrix. - The
b*Nterm comes from line endings, and any value separators you might be using. - The
cterm comes from file metadata, and any spare characters you might have in your file.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | francescalus |
| Solution 2 | veryreverie |
