nprimem
@nprimem (与太郎)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

文字列の操作について

以下のような操作を行う最も早い方法を知りたいです.

以下具体例のため0,1,...,9を用いますがそれぞれは文字だと思って下さい. また本番で実装するのは文字列の長さが2nであります.


1,2,3,4 ⇒ 1,4,2,3
1,2,3,4,5,6 ⇒ 1,6,2,5,3,4
0,1,2,3,4,5,6,7,8,9 ⇒ 0,9,1,8,2,7,3,6,4,5

のように与えられた文字列をAとすると

A⇒A[0],A[-1],A[1],A[-2],...のように頭、後ろ、頭、後ろ...と繰り返し並び返す.

今のところの僕の実装

def henkan(A):
   ans=""
   for i in range(len(A)//2):
      ans+=(A[i]+A[-(i+1)])
   return ans

この操作を何度も繰り返す数学の問題を考えています. なので仮に0.01%でも速度が向上すると嬉しいので些細な案でも教えていただけると幸いです.

0

1Answer

いろいろな実装を試してみましたが、処理する文字列の長さで処理速度順位が入れ替わりました。
OSやPythonのバージョンや環境によっても順位変動すると思います。

henkan.py
def henkan1(A):
    ans = ""
    for i in range(len(A) // 2):
        ans += (A[i] + A[-(i + 1)])
    return ans


def henkan2(A):
    ans = ""
    for i in range(len(A) // 2):
        ans = ans + (A[i] + A[-(i + 1)])
    return ans


def henkan3(A):
    ans = ""
    for i, c in enumerate(A[:len(A) // 2], 1):
        ans += c + A[-i]
    return ans


def henkan4(A):
    ans = ""
    for i, c in enumerate(A[-1:-len(A) // 2 - 1:-1]):
        ans += A[i] + c
    return ans


def henkan5(A):
    return "".join([c + A[-i] for i, c in enumerate(A[:len(A) // 2:], 1)])


def henkan6(A):
    return "".join([A[i] + c for i, c in enumerate(A[-1:-len(A) // 2 - 1:-1])])


def henkan7(A):
    a = [None] * len(A)
    a[::2] = A[:len(A) // 2]
    a[1::2] = A[-1:-len(A) // 2 - 1:-1]
    return "".join(a)


if __name__ == "__main__":
    text = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    expect = "aZbYcXdWeVfUgThSiRjQkPlOmNnMoLpKqJrIsHtGuFvEwDxCyBzA"
    print(henkan1(text) == expect, henkan1(text))
    print(henkan2(text) == expect, henkan2(text))
    print(henkan3(text) == expect, henkan3(text))
    print(henkan4(text) == expect, henkan4(text))
    print(henkan5(text) == expect, henkan5(text))
    print(henkan6(text) == expect, henkan6(text))
    print(henkan7(text) == expect, henkan7(text))
$ python3 -m timeit -s "from henkan import henkan1 as henkan" "henkan('0123')"
500000 loops, best of 5: 863 nsec per loop
$ python3 -m timeit -s "from henkan import henkan2 as henkan" "henkan('0123')"
500000 loops, best of 5: 859 nsec per loop
$ python3 -m timeit -s "from henkan import henkan3 as henkan" "henkan('0123')"
500000 loops, best of 5: 904 nsec per loop
$ python3 -m timeit -s "from henkan import henkan4 as henkan" "henkan('0123')"
500000 loops, best of 5: 911 nsec per loop
$ python3 -m timeit -s "from henkan import henkan5 as henkan" "henkan('0123')"
200000 loops, best of 5: 1.19 usec per loop
$ python3 -m timeit -s "from henkan import henkan6 as henkan" "henkan('0123')"
200000 loops, best of 5: 1.18 usec per loop
$ python3 -m timeit -s "from henkan import henkan7 as henkan" "henkan('0123')"
200000 loops, best of 5: 1.3 usec per loop
$ python3 -m timeit -s "from henkan import henkan1 as henkan" "henkan('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')"
50000 loops, best of 5: 7.51 usec per loop
$ python3 -m timeit -s "from henkan import henkan2 as henkan" "henkan('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')"
50000 loops, best of 5: 7.57 usec per loop
$ python3 -m timeit -s "from henkan import henkan3 as henkan" "henkan('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')"
50000 loops, best of 5: 7.01 usec per loop
$ python3 -m timeit -s "from henkan import henkan4 as henkan" "henkan('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')"
50000 loops, best of 5: 6.49 usec per loop
$ python3 -m timeit -s "from henkan import henkan5 as henkan" "henkan('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')"
50000 loops, best of 5: 6.47 usec per loop
$ python3 -m timeit -s "from henkan import henkan6 as henkan" "henkan('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')"
50000 loops, best of 5: 6.02 usec per loop
$ python3 -m timeit -s "from henkan import henkan7 as henkan" "henkan('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')"
100000 loops, best of 5: 3.19 usec per loop
1Like

Comments

  1. @nprimem

    Questioner

    ありがとうございます!
    初めて知れたことも多く本当に参考になりました

Your answer might help someone💌