0

How do you optimize this three snippets of code? Especially the third because there are many combinations between the list values and the time is dangerously longer with 1000 inputs.

Code 1:

e00=[]
for i in range(len(c1)):
    for j in range(len(d1[i])):
        if d1[i][j]%2==0:
           d = [c1[i],d1[i][j]]
           e00.append(d)

Code 2:

sciezki=[]
for i in range(len(out2)):     
    x1 = out2[i][-len(out2[i])]
    x2 =out2[i][-1]    
    z1 = nx.shortest_path(g, x1, x2)  
    if z1 == out2[i] and len(z1)==8:
          sciezki.append(z1)

Code 3:

out=[] 
for h in range(len(k)):
    if len(out)!=0:
       k2 = [out, k[h]]
       for q in range(len(k2[0])):
           for w in range(len(k2[1])):
               r = list(chain(k2[0][q],k2[1][w]))
               p = [n for n, _ in groupby(r)]
               if len(p)==h+2:
                  out.append(p)  
    else:
       for i in range(len(k[0])):
           for j in range(len(k[1])):
               r = list(chain(k[0][i],k[1][j]))
               p = [n for n, _ in groupby(r)]
               if len(p)==3:
                   out.append(p)
10
  • 1
    Usually it is better to iterate over the list directly, so for ci in c, instead of for i in range(len(c)): Commented Nov 22, 2017 at 8:36
  • Start with analyzing the algorithm and see if you can make it faster. After that you turn to code details. Commented Nov 22, 2017 at 8:37
  • @klutt this is always the worst, the man sits a few days over the algorithm, and then you have to think about the possible algorithm change :)) Commented Nov 22, 2017 at 8:41
  • 1
    I meant: What's the goal of the algorithm? Commented Nov 22, 2017 at 8:55
  • 1
    Well, without addressing the underlying algorithm I doubt you'll gain much in performance. But there are certain obvious things, like trying to iterate directly over a list rather than using indexing, and oh, don't do r = list(chain(...)) just use r = chain(...). And another micro-optimization of course is to "in-line" the .append method call: out = []; append_out = out.append then use append_out(p) in place of out.append(p). But all of these will be very minor, your problem is algorithmic complexity. Commented Nov 22, 2017 at 9:20

1 Answer 1

1

Code 1

Use a conditional list comprehension together with enumeration:

e00 = [[c_val, d_val] 
       for i, c_val in enumerate(c1) 
       for d_val d1[i] 
       if not d_val % 2]

Code 2

Only slight modifications required.

g = ...  # Undefined in sample code.
sciezki = []
for out2_val in out2:     
    x1 = out2_val[-len(out2_val)]
    x2 = out2_val[-1]    
    z1 = nx.shortest_path(g, x1, x2)  
    if z1 == out2_val and len(z1) == 8:
        sciezki.append(z1)
        x = '\n'.join(map(str,sciezki))  # Remove if possible. 
Sign up to request clarification or add additional context in comments.

1 Comment

Właściwie to 'x' tutaj jest niepotrzebne, mój punkt widzenia. 'g' to g = nx.Graph () z Networkx. 2 kod polega na porównaniu pierwszego i ostatniego węzła i przejścia przez "shortest_path", czy dany wybór jest jednoznaczny.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.