Timing several versions give interesting results:
program test
implicit none
integer :: i,k
logical :: multi
multi = command_argument_count() > 0
do k=1,10000000
if (multi) then
! WRITE(*,'(*(A1))') (achar(i),i=49,90)
! WRITE(*,'(42A1)') (achar(i),i=49,90)
WRITE(*,*) (achar(i),i=49,90)
else
do i=49,90
WRITE(*,"(A)",advance="no")achar(i)
enddo
print*,""
endif
enddo
end program test
>time ./a.out > /dev/null
68.902u 25.599s 1:34.50 99.9%
using @kargl's hint:
>time ./a.out 1 > /dev/null
7.908u 0.584s 0:08.49 99.8% using @kargl's hint, list-directed (*) format
12.923u 0.547s 0:13.47 99.9% as above, (42A1) format
14.673u 0.543s 0:15.21 100.0% as above, (*(A1)) format
@FortranFan’s version, with 10,000,000 executions of the inner loop, completes in
time ./stdio_m > /dev/null
2.236u 0.016s 0:02.25 99.5%
So, it seems, there is a huge overhead added by executing WRITE statement (43 times more in OP version vs. @kargl’s), less dependent on the actual number of characters output. NB., the Fortran-C version is much faster even with 430 million calls to putchar/puts
NOTE: there is a strange problem with Fortran-C version. Compiled with gfortran v. 10 outputs lines ending with extra ‘D’ character:
123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZD
ifort gives expected output ending each line on ‘Z’.
This can be fixed by modifying the @FortranFan’s program unit to
program stdio
use, intrinsic :: iso_c_binding, only : c_char, c_int
use stdio_m, only : putchar, puts
integer(c_int) :: i,k, r,nl
nl = iachar(new_line('a'))
do k=1,10000000
do i = 49, 90
r = putchar( i )
end do
r = putchar(nl)
end do
end program stdio
Is puts(char_c_"") OK?