Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Speed of writing formatted matrices of floats in Fortran and C++

43 views
Skip to first unread message

Beliavsky

unread,
May 6, 2022, 8:03:47 AM5/6/22
to
I am finding that writing 100x100 matrices of floats is about 25% faster with gfortran than g++ using printf on Windows and 4 times faster with gfortran than g++ on WSL2. I wonder what people see on Linux and if the C++ performance can be improved (or if the comparison is invalid for some reason). The codes and scripts are at https://github.com/Beliavsky/Formatted_output_speed . Maybe Fortran benefits because a whole row of a matrix can be written with

write (iu,"(*(1x,f0.6)") x(i,:)

instead of looping over each element.

Ron Shepard

unread,
May 6, 2022, 12:29:12 PM5/6/22
to
In addition to treating the i/o list as a sequence of scalars or as a
vector, there is also the question, in both languages, of how often the
format string is parsed. With FORMAT statements, compilers would
typically parse the format strings at compile time, so no run time
overhead occurred for that. Then when f77 allowed format strings with
character variables, there was sometimes significant differences in i/o
costs between the two approaches. This was because the format string was
parsed anew for each execution of the write statement. Then over time,
this was optimized by compilers. First, the literal strings and
character parameters were singled out and parsed at compile time the
same way as format statements. Then the compilers started recognizing
when variable strings were unchanged between write statements, and
optimized that parsing at run time.

I was in a computer users group back in the 1980s. Over a period of a
few years, there were frequent discussions about the costs of the
different fortran compilers on the IBM mainframe machines. Some users
were pushing for the compilers that supported f77, others wanted to keep
the old compilers because at first they were more efficient. My codes
would run for hours at a time and then print a few pages of output, so
my concern was the run time efficiency of the linear algebra, do loop
executions, and so on. Someone in another group presented results that
showed the f77 compiler was about 8x slower than the old f66+ compiler.
He always showed the ratios, not the actual times. This went on for
months. Then just by chance, someone asked him what were his actual
times. It turned out that they were executions of a few seconds each.
His jobs did a little bit of calculation and i/o, and then printed out
listings that were a couple hundred pages long. His computer times were
all dominated by i/o costs, which at that time were slower for the f77
compiler than they were for the old f66+ compiler. So people like me,
running jobs for hours at a time, were being held hostage by people
running codes that consumed a few seconds of time, and it was over
trivial issues like i/o library runtime performance.

The joys of shared computing on mainframes.

$.02 -Ron Shepard

Beliavsky

unread,
May 6, 2022, 12:43:17 PM5/6/22
to
Great story. Please consider also posting it at https://fortran-lang.discourse.group/t/anecdotal-fortran/704 .
0 new messages