LoginSignup
1
0

More than 1 year has passed since last update.

[py2rb] double splat引数 と 実行速度の検証

Last updated at Posted at 2022-01-05

はじめに

移植やってます。
( from python 3.7 to ruby 2.7 )

double splat引数 (Python)

def charge(sequence, pH, **kwargs):
# 略

class ElectrochemTest(unittest.TestCase):
    def test_charge_calculations_str(self):
        self.assertTrue(
            abs(charge('AAA', 5.0,
                       pK={'H-': [(9., 1)], '-OH': [(8., -1)]},
                       pK_nterm={'H-': {'A': [(3., 1)]}})) < 0.01)

pKpK_ntermって仮引数にないのですが、実は辞書のキーでした。

double splat引数 (Ruby)

def charge(sequence, pH, **kwargs)
# 略
end

class TestEctrochem < Minitest::Test
  def test_charge_calculations_str
    assert charge(
      'AAA', 5.0,
        'pK' => {'H-' => [[9.0, 1]], '-OH' => [[8.0, -1]]},
        'pK_nterm' => {'H-' => {'A' => [[3.0, 1]]}}
    ).abs < 0.01
  end
end
def charge(sequence, pH, **kwargs)
# 略
end

class TestEctrochem < Minitest::Test
  def test_charge_calculations_str
    assert charge(
      'AAA', 5.0,
+      {
        'pK' => {'H-' => [[9.0, 1]], '-OH' => [[8.0, -1]]},
        'pK_nterm' => {'H-' => {'A' => [[3.0, 1]]}}
+      }
    ).abs < 0.01
  end
end
def charge(sequence, pH, **kwargs)
# 略
end

class TestEctrochem < Minitest::Test
  def test_charge_calculations_str
    assert charge(
      'AAA', 5.0,
+      **{
        'pK' => {'H-' => [[9.0, 1]], '-OH' => [[8.0, -1]]},
        'pK_nterm' => {'H-' => {'A' => [[3.0, 1]]}}
+      }
    ).abs < 0.01
  end
end

{}なし{}あり**{}の3つを書きましたが、この内{}ありはエラーが発生するので、コントリビュートチャンスだそうです。

ArgumentError: wrong number of arguments (given 3, expected 2)

速度の検証

こちらの記事によりますと、double splatは遅いらしいです。

minitestの結果を参照しますと、レシーバ側はkwargs = {}、呼び出し側は{}なしが速そうです。Ruby 3 何?それっておいしいの?

**kwargs kwargs = {}
{}なし 2736.8176 assertions/s 3226.1533 assertions/s
**{} 3005.5192 assertions/s 3190.0239 assertions/s

メモ

  • Python の double splat引数 を学習した
  • 百里を行く者は九十里を半ばとす
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0