developer tip

C #의 StringBuilder와 같은 Python 문자열 클래스?

optionbox 2020. 8. 15. 09:06
반응형

C #의 StringBuilder와 같은 Python 문자열 클래스?


StringBuilderC # 과 같이 Python에 문자열 클래스가 있습니까?


일대일 상관 관계가 없습니다. 정말 좋은 기사를 보려면 Python의 효율적인 문자열 연결을 참조하십시오 .

Python 프로그래밍 언어로 긴 문자열을 빌드하면 코드 실행 속도가 매우 느려질 수 있습니다. 이 기사에서는 다양한 문자열 연결 방법의 계산 성능을 조사합니다.


Oliver Crow (Andrew Hare가 제공 한 링크)의 코드를 사용하고 Python 2.7.3을 조정하기 위해 약간 수정했습니다. (timeit 패키지 사용). 개인용 컴퓨터 인 Lenovo T61, 6GB RAM, Debian GNU / Linux 6.0.6 (squeeze)에서 실행했습니다.

다음은 10,000 회 반복에 대한 결과입니다.

method1 : 0.0538418292999 초
프로세스 크기 4800 kb
method2 : 0.22602891922 초
프로세스 크기 4960 kb
method3 : 0.0605459213257 초
프로세스 크기 4980 kb
method4 : 0.0544030666351 초
프로세스 크기 5536 kb
method5 : 0.0551080703735 초
프로세스 크기 5272 kb
method6 : 0.0542731285095 초
프로세스 크기 5512kb

5,000,000 회 반복 (영원히 너무 느리게 실행 되었기 때문에 메서드 2가 무시되었습니다) :

method1 : 5.88603997231 초
프로세스 크기 37976 kb
method3 : 8.40748500824 초
프로세스 크기 38024 kb
method4 : 7.96380496025 초
프로세스 크기 321968 kb
method5 : 8.03666186333 초
프로세스 크기 71720 kb
method6 : 6.68192911148 초
프로세스 크기 38240 kb

파이썬 사람들이 문자열 연결을 최적화하기 위해 꽤 훌륭한 일을했다는 것은 분명합니다. 그리고 Hoare가 말했듯이 : "조기 최적화는 모든 악의 근원입니다":-)


파이썬에는 비슷한 목적을 수행하는 몇 가지가 있습니다.

  • 조각에서 큰 문자열을 만드는 일반적인 방법 중 하나는 문자열 목록을 늘리고 완료되면 결합하는 것입니다. 이것은 자주 사용되는 Python 관용구입니다.
    • 데이터를 형식화와 통합하는 문자열을 작성하려면 형식화를 별도로 수행해야합니다.
  • 문자 수준에서 삽입 및 삭제하려면 길이가 1 인 문자열 목록을 유지합니다. (문자열에서 만들려면을 호출 list(your_string)합니다. UserString.MutableString이를 위해 a 사용할 수도 있습니다 .
  • (c)StringIO.StringIO 그렇지 않으면 파일을 가져 오는 것에 유용하지만 일반적인 문자열 작성에는 덜 유용합니다.

컴파일러 최적화에 의존하는 것은 취약합니다. Antoine-tran이 제공하는 허용 된 답변과 숫자에 연결된 벤치 마크는 신뢰할 수 없습니다. Andrew Hare는 repr자신의 메서드에를 호출하는 실수를 범 합니다. 이는 모든 메서드를 똑같이 느리게하지만 문자열을 구성 할 때의 실제 패널티를 모호하게합니다.

사용 join. 매우 빠르고 강력합니다.

$ ipython3
Python 3.5.1 (default, Mar  2 2016, 03:38:02) 
IPython 4.1.2 -- An enhanced Interactive Python.

In [1]: values = [str(num) for num in range(int(1e3))]

In [2]: %%timeit
   ...: ''.join(values)
   ...: 
100000 loops, best of 3: 7.37 µs per loop

In [3]: %%timeit
   ...: result = ''
   ...: for value in values:
   ...:     result += value
   ...: 
10000 loops, best of 3: 82.8 µs per loop

In [4]: import io

In [5]: %%timeit
   ...: writer = io.StringIO()
   ...: for value in values:
   ...:     writer.write(value)
   ...: writer.getvalue()
   ...: 
10000 loops, best of 3: 81.8 µs per loop

위의 방법 5 (의사 파일)를 사용하면 매우 우수한 성능과 유연성을 얻을 수 있습니다.

from cStringIO import StringIO

class StringBuilder:
     _file_str = None

     def __init__(self):
         self._file_str = StringIO()

     def Append(self, str):
         self._file_str.write(str)

     def __str__(self):
         return self._file_str.getvalue()

지금 그것을 사용

sb = StringBuilder()

sb.Append("Hello\n")
sb.Append("World")

print sb

StringIO 또는 cStringIO를 시도 할 수 있습니다.


There is no explicit analogue - i think you are expected to use string concatenations(likely optimized as said before) or third-party class(i doubt that they are a lot more efficient - lists in python are dynamic-typed so no fast-working char[] for buffer as i assume). Stringbuilder-like classes are not premature optimization because of innate feature of strings in many languages(immutability) - that allows many optimizations(for example, referencing same buffer for slices/substrings). Stringbuilder/stringbuffer/stringstream-like classes work a lot faster than concatenating strings(producing many small temporary objects that still need allocations and garbage collection) and even string formatting printf-like tools, not needing of interpreting formatting pattern overhead that is pretty consuming for a lot of format calls.


In case you are here looking for a fast string concatenation method in Python, then you do not need a special StringBuilder class. Simple concatenation works just as well without the performance penalty seen in C#.

resultString = ""

resultString += "Append 1"
resultString += "Append 2"

See Antoine-tran's answer for performance results

참고URL : https://stackoverflow.com/questions/2414667/python-string-class-like-stringbuilder-in-c

반응형