本帖最后由 weixing1531 于 2024-4-1 12:39 编辑
在Fortran wiki 上看到这个有趣的代码
测试了一下,快速排序慢3倍,堆排序慢35%
[Fortran] 纯文本查看 复制代码 module M_swap
private
public swap
contains
subroutine swap(lhs,rhs)
use iso_c_binding
implicit none
class(*),intent(inout) :: lhs, rhs
class(*), allocatable :: temp
! Copy N bytes of SRC to DEST, no aliasing or overlapping allowed.
! extern void *memcpy (void *dest, const void *src, size_t n);
interface
subroutine s_memcpy(dest, src, n) bind(C,name='memcpy')
use iso_c_binding
integer(c_intptr_t), value, intent(in) :: dest
integer(c_intptr_t), value, intent(in) :: src
integer(c_size_t), value :: n
end subroutine s_memcpy
end interface
temp=lhs
call s_memcpy(loc(lhs), loc(rhs), storage_size(lhs, kind=c_size_t)/8_c_size_t )
call s_memcpy(loc(rhs), loc(temp), storage_size(rhs, kind=c_size_t)/8_c_size_t )
end subroutine swap
end module M_swap
|