textwrap.wrap()
과 textwrap.fill()
의 속도가 느린 이유는 무엇입니까? 예를 들어, 랩탑에 10000 자의 문자열을 감싸는 데는 거의 2 초 30 초가 걸립니다.왜 textwrap.wrap() 및 textwrap.fill()이 느린가요?
$ python -m timeit -n 10 -s 's = "A" * 10000; import textwrap' 'textwrap.fill(s)'
10 loops, best of 3: 2.41 sec per loop
textwrap
보다 빠르게 크기의 텍스트 주문을 래핑 an answer to a related Stack Overflow question
#!/usr/bin/env python
# simplewrap.py
def fill(text, width=70):
return '\n'.join(text[i:i+width] for i in
range(0, len(text), width))
에서 적응이 코드에 그 비교 :
$ python -m timeit -n 10 -s 's = "A" * 10000; import simplewrap' 'simplewrap.fill(s)'
10 loops, best of 3: 37.2 usec per loop
을 스크립트는 지원하며 간단한 코드는 문자열을 길이가 70 인 배열로 맹목적으로 나눕니다. – hamstergene