- くだすれFORTRAN(超初心者用)その6
694 :デフォルトの名無しさん[sage]:2014/12/20(土) 11:31:57.54 ID:9/Q8xBDD - 『if … else if … end if』と『select case …』はどっちが速いのか,ふと思って計測してみた.
cygwin gcc 4.8.3@core-i5で繰り返し計測してみたけど,時間差はばらつきの範囲内で有意な差はなさそうな感じ. 二つの構文ともCPU時間同じ,機能も同じで,いまいち,使い分け方が分からないんだよねぇ... 使ったコードはこれ↓ module mod_util implicit none integer(kind=4) :: t1, t2, t_rate, t_max, t_diff contains real(8) function elapsedTime() implicit none call SYSTEM_CLOCK(t2, t_rate, t_max) if ( t2 < t1 ) then t_diff = t_max - t1 + t2 else t_diff = t2 - t1 endif elapsedTime = t_diff/real(t_rate) end function elapsedTime end module mod_util
| - くだすれFORTRAN(超初心者用)その6
695 :694[sage]:2014/12/20(土) 11:33:00.40 ID:9/Q8xBDD - program testSelectIf
use mod_util, only : t1, elapsedTime implicit none real (kind=8) :: randX integer(kind=4) :: i, j, randI #ifdef ifelse write(*,'("# if ... else if ... ")') #else write(*,'("# select case")') #endif call init_randomSeed(972384) call SYSTEM_CLOCK(t1) do i = 1, 100000000 call random_number(randX) randI = int( randX * 10, kind=4 ) #ifdef ifelse if ( randI == 1 ) then; j = j + 1 else if ( randI == 2 ) then; j = j - 2 else if ( randI == 3 ) then; j = j + 3 else if ( randI == 4 ) then; j = j - 4 else if ( randI == 5 ) then; j = j + 5 else if ( randI == 6 ) then; j = j - 6 else if ( randI == 7 ) then; j = j + 7 else if ( randI == 8 ) then; j = j - 8 else if ( randI == 9 ) then; j = j + 9 else if ( randI ==10 ) then; j = j - 10
| - くだすれFORTRAN(超初心者用)その6
696 :694[sage]:2014/12/20(土) 11:33:45.85 ID:9/Q8xBDD - end if
#else select case (randI) case ( 1 ); j = j + 1 case ( 2 ); j = j - 2 case ( 3 ); j = j + 3 case ( 4 ); j = j - 4 case ( 5 ); j = j + 5 case ( 6 ); j = j - 6 case ( 7 ); j = j + 7 case ( 8 ); j = j - 8 case ( 9 ); j = j + 9 case ( 10 ); j = j - 10 end select #endif end do write(*,'("j=",I0)') j write(*,'("#time:", F10.3)') elapsedTime() end program testSelectIf
|
|