Skip to content

O(n^2) string concatenation #1

@shmulvad

Description

@shmulvad

Hi,

I randomly came across your blog post on this library and thought I'd take a look at the code. I noticed you have an accidental O(n^2) string concatenation. In a few places in your code, you're appending lists of strings. This is a problem as it creates new intermediate strings in each iteration. Here is one such example:

Fontimize/fontimize.py

Lines 186 to 190 in 317f1e1

def optimise_fonts_for_html_contents(html_contents : list[str], fonts : list[str], fontpath : str = "", subsetname = "FontimizeSubset", verbose : bool = False, print_stats : bool = True) -> dict[str, typing.Any]:
text = ""
for html in html_contents:
soup = BeautifulSoup(html, 'html.parser')
text = text + soup.get_text()

A more efficient way to write the above would be something like the following as it creates a single new string object:

text = ''.join(BeautifulSoup(html, 'html.parser').get_text() for html in html_contents)

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions