question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Revert converting str.join(listcomp) to str.join(generator)

See original GitHub issue

Consider reverting the listcomp->generator for the str.join() case, it’s faster to use a list comprehension than a generator in this case:

$ python -m timeit '"y".join("x" for _ in range(1000000))'
5 loops, best of 5: 40.6 msec per loop
$ python -m timeit '"y".join(["x" for _ in range(1000000)])'
10 loops, best of 5: 29.6 msec per loop

This is because str.join() constructs a list internally and short-circuits if passed an already created list.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MarcoGorellicommented, Sep 24, 2021

Having tried this out, it looks like even that one’s slower:

In [8]: %timeit frozenset([i for i in range(1000)])
36.6 µs ± 661 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

In [9]: %timeit frozenset((i for i in range(1000)))
41.8 µs ± 940 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

I’d suggested this feature, and it seems that in several cases it induces a small performance hit, so…I’m sorry about that

@asottile reckon it’s worth removing this rewrite altogether?

0reactions
sethmlarsoncommented, Sep 24, 2021

@MarcoGorelli Thanks for making my eyes actually work, sorry for the noise.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Joining strings. Generator or list comprehension?
The call to PySequence_Fast converts the seq argument into a list if it wasn't a list or tuple already. So, the two versions...
Read more >
Convert list to string in python using join() / reduce() / map()
""" Convert list to string, by joining all item in list with given separator. Returns the concatenated string """. return seperator.
Read more >
How to Use Python's Join() on a List of Objects (Not Strings)?
The most Pythonic way to concatenate a list of objects is the expression ''.join(str(x) for x in lst) that converts each object to...
Read more >
Python | Convert List of String List to String List - GeeksforGeeks
In this, we join the numbers using join and construct a string integers. ... join(). List comprehension is used to iterate through the...
Read more >
Python's map(): Processing Iterables Without a Loop
You'll also learn how to use list comprehension and generator expressions to replace map() in a ... Transforming Iterables of Strings With Python's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found