My Fortran rotating star and cost of screen output.

Ignore this post.

Got a Fortran program that does some numerical work and I just have it outputting a single character (‘-‘) to the screen each cycle, so I know it’s ticking along. It looks like this, sort of:

--------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------> done
Whoa! Exciting.

Whoa! Exciting. This was made using gifsicle.

But I thought that was ugly, so I’ve got something much nicer now. You know how in some text-based programs the fact that it has not completely locked up is shown by having little rotating bar, made just by printing |, /, -, and \ in the same place one after the other? Well, this is one way to do it in Fortran (note, just a snippet, not a working program):

! declare array 'star()'

       character,dimension(1) :: star(4)     

       star(1) = '|'
       star(2) = '/'
       star(3) = '-'
       star(4) = '\'

!.
!.
!other code...
!.
!.

       do iloop=1,nloop
          write(6,'(a,a)',advance='no')achar(8),star(mod(iloop,4)+1)
!.
!.
!content of loop...
!.
!.
       end do

So all that this does is write an ‘achar(8)’ character to make the output backspace, use “advance=’no'” to avoid adding a line return, and use ‘mod’ to run through the four characters in the array over and over again.

‘achar(13)’ also works but only if you want to return to the beginning of the line and overwrite everything.

While thinking about this, I wondered: The loop is fairly big (searches some big arrays, does some calculations, works out an energy in a Monte Carlo simulation, that sort of thing) and so since this starry thing is the only screen output, I was wondering — how much does it cost in runtime?

using Gfortran, with -O2 optimisation. With and without the above ‘write’ statement, times are:

With:

 $ time ./Dom2016G_tests < inputsG 

real	2m50.757s
user	1m57.032s
sys	0m1.616s

Without:

real	2m49.191s
user	1m56.895s
sys	0m1.587s

So there we have it, a time cost (looking at the ‘user’ time) of less than 0.2s, and only about 0.1%. So I think I’ll keep it. It looks nice.

Oh, and making the animated gif: I made four little bitmaps by screen grabs from LibreOffice, all the same size, and called them a1.gif, a2.gif a3.gif and a4.gif. Then just this command line (I put loopcount in to stop it after a finite number of loops. –loop and it goes forever…):

gifsicle --loopcount=100 -d 30  a?.gif > star.gif

a2
a4a3
a1

 

 

 

 

 

Rubbish.

Advertisements

Tags: , , , ,

About Darren

I'm a scientist by training, based in Australia.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: