Improving Fortran Results in the Julia Micro-benchmarks

The hex_string subroutine in perf.f90 generates a hex representation of a 32-bit integer. The present implementation uses modulo() and integer divisions by 16 to generate the hex string, which is then used as the input for the parsing routine. The following replacement uses shifts and masks, instead, and on my PC reduced the parse_integers microbenchmark time from 0.130 s to 0.047 s. I have offered this replacement code as an issue on the Julia microbenchmarks Github.

! Convert an integer to a hex string
!
subroutine hex_string(dec,hexchar)
  integer, intent(in) :: dec
  character(*) :: hexchar

  integer :: i, quotient

  character(len=1), parameter :: table(0:15) = &
  [(char(i),i=ichar('0'),ichar('9')),(char(i),i=ichar('A'),ichar('F'))]

  quotient = dec
  hexchar  = '00000000'
  i = 8
  do while (quotient /= 0 .and. i > 0)
      hexchar(i:i) = table(iand(quotient,15))
      i = i-1
      quotient = ishft(quotient,-4)
  end do

end subroutine hex_string
3 Likes