-
-
Notifications
You must be signed in to change notification settings - Fork 391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Forbid slow in
inside comprehensions
#864
Comments
Good idea! But, we don't assume types as the best practice. # ex.py
len(1 for el in a if el in b) Output:
But, We can forbid to use |
in
inside comprehensions
We need to measure the speed for different types with |
From worst to best: from random import randint
elements = list(randint(-100000, 100000) for _ in range(1000000))
%timeit sum(a > 0 for a in elements)
# 130 ms ± 3.15 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit sum(True for a in elements if a > 0)
# 92.1 ms ± 2.62 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
%%timeit
s = 0
for a in elements:
if a > 0:
s += 1
# 73.1 ms ± 714 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit sum(1 for a in elements if a > 0)
# 71.8 ms ± 720 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
%timeit len([1 for a in elements if a > 0])
# 62.4 ms ± 9.78 ms per loop (mean ± std. dev. of 7 runs, 10 loops each) |
The last one has a big std deviation. So, I'm not sure is it really the best or some optimization for repetitive list creation. |
Would like to take this! Can u plz assign this to me |
So, which one should be considered as a best practice?? |
It means that it is better to use |
So, I'm done with the local setup and ready to implement the changes. But, I'm quite confused about how should I approach the issue... |
@ManishAradwad you need to create a new violation in We need to visit:
You can add a new method in the visitor: Here's how our bad node (
Then you write the required logic, test it, and submit a PR. I am here to help. |
@ManishAradwad sorry for misleading you. This is a refactoring violation. |
We are using both Then what do u mean by
|
Also are you saying that I should add the function |
We don't check |
Hi @ManishAradwad, how's it going? Do you need any help? |
This should be joined with #1008 |
I think some clarification on exactly what is forbidden is needed here. I've just read through this twice, and I'm still not clear. Are we forbidding slow sum/len calls (in which case we do need to check the functions), or something in all comprehensions? Because some of the bad examples don't make sense to forbid outside of a len/sum call. e.g. |
Rule request
Thesis
len with comprehensions
Iterators have no
len
, and sometimes I forgetting it.Bad:
Better:
in
inside comprehensionsGood:
Twice slower, but also ok:
Reasoning
Detect runtime
TypeError
in advance. We could also detectlen
fromyield
-like iterators, but resolving symbols in python always is a non-trivial thing, unfortunately.The text was updated successfully, but these errors were encountered: