Hello!

It's Friday and time for a little question which I wondered myself a while now. mecej4 wrote in another thread:

Expressions that involve the squaring of real variables are more efficiently handled by keeping the exponent (=2) as an integer and evaluating the power by multiplication. Promoting integer exponents to real would require the use of the mathematical equivalences

x

^{y}= exp(y*log_{e}x) = 2^{y*log2x}Evaluation of the transcendental functions may be much slower, and problems could arise if the variable x were not positive.

To test the impact on performance and precision I wrote a litle program:

! ############################################################### ! ### This is a little program to test the exponent precision ### ! ############################################################### program exponent_precision use ifport implicit none integer :: i integer :: i_exponent integer, parameter :: int_count = 100000000 integer, parameter :: dp = kind(1.d0) real(dp) :: result_iex(int_count), result_rex(int_count) real(dp) :: r_exponent real(dp) :: diff real(dp) :: t1, t2, t3 ! --------------------------------------------------------------------- i_exponent = 4 r_exponent = dble(i_exponent) result_iex(1) = 1000.d0**i_exponent result_rex(1) = 1000.d0**r_exponent ! Difference in per mille diff = (result_rex(1) - result_iex(1))/result_rex(1) * 1000.d0 write(*,*) 'Difference between results: ',diff ! calculation time difference t1 = dclock() do i = 1, int_count result_iex(i) = (1000.1d0+dble(i))**i_exponent end do t2 = dclock() do i = 1, int_count result_rex(i) = (1000.1d0+dble(i))**r_exponent end do t3 = dclock() write(*,*) 'Elapsed time integer exponent in sec: ',t2-t1 write(*,*) 'Elapsed time real exponent in sec: ',t3-t2 write(*,*) 'Factor: ', (t3-t2)/(t2-t1) read(*,*) ! --------------------------------------------------------------------- end program exponent_precision

maybe not elegant, but the results are not as I would expect:

The result with default debug (WIN32) project settings (ifort 12.1.6.369) is:

Difference between results: 0.000000000000000E+000

Elapsed time integer exponent in sec: 2.45599999999831

Elapsed time real exponent in sec: 6.14600000000064

Factor: 2.50244299674465

Now the result with default release (WIN32) project settings (ifort 12.1.6.369) is:

Difference between results: 0.000000000000000E+000

Elapsed time integer exponent in sec: 0.000000000000000E+000

Elapsed time real exponent in sec: 3.10200000000623

Factor: Infinity

In both cases the kind of the exponent (double precision, integer) has no impact on the precision of the results. The performance difference in debug is that the calcultion with an integer exponent is about 2.5 times faster (in this special case on my Xeon X5 machine!!). Not bad to know. And now comes what I have not expected. With activated optimization (Maximize Speed) the calculation time with integer exponent is below a measurable value for dclock and so the factor becomes infinity. The optimizer made a good job! I don't know the impact in a real program but I for myself will prefor the integer exponent for future.

Kind regrads,

Johannes