Fortran Coder

查看: 150|回复: 0
打印 上一主题 下一主题

一个通用的swap子程序

[复制链接]

127

帖子

35

主题

1

精华

大师

F 币
1153 元
贡献
592 点
跳转到指定楼层
楼主
发表于 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, 2024-5-2 02:43

Powered by Tencent X3.4

© 2013-2024 Tencent

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