syhsyh9696
V2EX  ›  Ruby

Ruby 破环方法为什么比非破坏方法慢一些?

  •  1
     
  •   syhsyh9696 · Mar 5, 2017 · 8013 views
    This topic created in 3357 days ago, the information mentioned may be changed or developed.

    做 leetcode 的时候我发现

    nums.sort! 明显要比 nums = nums.sort 要慢 10ms

    这是为什么呢? sort!的操作更多?虽然 sort!写起来格外的方便,但是吃了很多时间的亏。

    2 replies    2017-03-05 19:17:25 +08:00
    ch3nz
        1
    ch3nz  
       Mar 5, 2017
    我怀疑是 leetcode 的问题
    Array.sort 的实现其实是
    1 ) 复制自己得到一个副本
    2 )调用副本的 sort !
    3 )返回结果
    所以要真说慢,那也是 sort 更慢。

    https://github.com/ruby/ruby/blob/trunk/array.c#L2540
    dant
        2
    dant  
       Mar 5, 2017

    并没有什么区别
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1263 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 23:39 · PVG 07:39 · LAX 16:39 · JFK 19:39
    ♥ Do have faith in what you're doing.