Fortran Coder

查看: 622|回复: 0

一个通用的swap子程序

[复制链接]

147

帖子

42

主题

1

精华

宗师

F 币
1302 元
贡献
630 点
发表于 2024-4-1 12:17:45 | 显示全部楼层 |阅读模式
本帖最后由 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

您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2025-1-14 13:22

Powered by Tencent X3.4

© 2013-2025 Tencent

快速回复 返回顶部 返回列表