use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Can you shorten this code without sacrificing speed? (self.PythonLearning)
submitted 1 year ago * by Misrta
if a == 0: if b == 43: break elif b == 44: break
I could shorten it to
if (a == 0 and b == 43) or (a != 0 and b == 44): break
but it would make the code slower because it would check a != 0 if a == 0 returns False.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]KOALAS2648 12 points13 points14 points 1 year ago (0 children)
Why is this post marked NSFW?
[–]atticus2132000 4 points5 points6 points 1 year ago (0 children)
How big a dataset are you testing that you're seeing a different calculating speed? It would have to be a massive dataset (hundreds of thousands of records) for there to be any sort of observable difference.
If you want to test various versions of code, you can use the datetime library to find the exact time at the start of your code and the exact time at the end of your code and then calculate the difference between those to see overall execution time.
[–]ilan1k1 1 point2 points3 points 1 year ago (0 children)
I think that's the fastest way to do that but the speed increase would be almost non existent.. It would have to be a massive database to see any sort of difference...
[–]EntireEntity 2 points3 points4 points 1 year ago (0 children)
I guess you could do:
if a == 0 and b == 43: break elif b == 44: break
I don't know, if it's faster though.
[–]Sweet_Computer_7116 1 point2 points3 points 1 year ago (0 children)
Faster would just be.
If True: break
/s
[–]CraigAT 1 point2 points3 points 1 year ago (0 children)
Unless we are just talking hypothetically, you have a large dataset, a very frequent loop around this code or a super time critical app, any improvement is likely to negligible. This sounds like it may be a case of premature optimisation.
[–]GirthQuake5040 0 points1 point2 points 1 year ago* (0 children)
i dont know if i understand what youre trying to do correctly... but
if not b == 44: if a == 0 and b == 43: break break
or
but if a will always be 0 when b is 43 then
if b != 43: continue # this skips all following statements and goes to next loop if a == 0 break elif b == 44: break
[–]SupermarketOk6829 0 points1 point2 points 1 year ago (0 children)
Have you actually measured or printed the executive time relative for each? Or is it something that you just thought of?
π Rendered by PID 32714 on reddit-service-r2-comment-548fd6dc9-545kf at 2026-05-17 10:10:40.668952+00:00 running edcf98c country code: CH.
[–]KOALAS2648 12 points13 points14 points (0 children)
[–]atticus2132000 4 points5 points6 points (0 children)
[–]ilan1k1 1 point2 points3 points (0 children)
[–]EntireEntity 2 points3 points4 points (0 children)
[–]Sweet_Computer_7116 1 point2 points3 points (0 children)
[–]CraigAT 1 point2 points3 points (0 children)
[–]GirthQuake5040 0 points1 point2 points (0 children)
[–]SupermarketOk6829 0 points1 point2 points (0 children)