On 2023-11-15 12:26:32 +0200, Dom Grigonis wrote: > > Thank you, > > > test2 = [True] * 100 + [False] * 2 > test2i = list(range(100)) > > %timeit len(set(test2i)) == 1 # 1.6 µs ± 63.6 ns per loop (mean ± std. dev. > of 7 runs, 1,000,000 loops each) > %timeit all(test2) # 386 ns ± 9.58 ns per loop (mean ± std. dev. > of 7 runs, 1,000,000 loops each) > > test2s = set(test2i) > %timeit len(test2s) == 1 # 46.1 ns ± 1.65 ns per loop (mean ± std. > dev. of 7 runs, 10,000,000 loops each) > > If you pre-convert to set it is obviously faster. However, set > operation is most likely going to be part of the procedure. In which > case it ends up to be significantly slower.
Obviously, if you convert a list to a set just to count the elements it's going to be slow. My suggestion was to use the set *instead* of the list. I don't know whether that's possible in your situation, because you haven't told us anything about it. All I'm suggesting is taking a step back and reconsider your choice of data structure. hp -- _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | h...@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!"
signature.asc
Description: PGP signature
-- https://mail.python.org/mailman/listinfo/python-list