Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code optimization
#2
Use combinations to reduce the number of pairs you have to test. You'll have to change the test to check for +/-h. Using list.count() and a list comprehension should speed things up too.
def countit2(values, h=1):
     return [abs(a-b) for a, b in itertools.combinations(set(values), r=2)].count(h)
Reply


Messages In This Thread
Code optimization - by Vidar567 - Nov-24-2020, 09:03 PM
RE: Code optimization - by deanhystad - Nov-24-2020, 10:17 PM
RE: Code optimization - by Vidar567 - Nov-24-2020, 10:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Portfolio Optimization code error i want to cry FinanceMotta 3 2,660 Mar-11-2024, 10:24 PM
Last Post: deanhystad
  Multithreading with queues - code optimization h1v3s3c 1 3,409 May-10-2018, 10:40 AM
Last Post: ThiefOfTime

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020