vendredi 23 août 2019

Speed improving by Elif statements decreasing

As I'm working with huge data. My code is running for 10+ hours. I was trying to decrease "elif" through the other questions on the stackoverflow, but I have failed.

Initial code is:

def _c(ca, i, j, p, q):

    if ca[i, j] > -1:
        return ca[i, j]
    elif i == 0 and j == 0:
        ca[i, j] = np.linalg.norm(p[i]-q[j])
    elif i > 0 and j == 0:
        ca[i, j] = max(_c(ca, i-1, 0, p, q), np.linalg.norm(p[i]-q[j]))
    elif i == 0 and j > 0:
        ca[i, j] = max(_c(ca, 0, j-1, p, q), np.linalg.norm(p[i]-q[j]))
    elif i > 0 and j > 0:
        ca[i, j] = max(
            min(
                _c(ca, i-1, j, p, q),
                _c(ca, i-1, j-1, p, q),
                _c(ca, i, j-1, p, q)
            ),
            np.linalg.norm(p[i]-q[j])
            )
    else:
        ca[i, j] = float('inf')



     return ca[i, j]

I have tried to apply dictionaries. My try was:

id_to_name = {"-1": "ca[i, j]",
              "i == 0 and j == 0": "np.linalg.norm(p[i]-q[j])",
              "i > 0 and j == 0", "max(_c(ca, i-1, 0, p, q),np.linalg.norm(p[i]-q[j]))",
              "i == 0 and j > 0",:"max(_c(ca, 0, j-1, p, q), np.linalg.norm(p[i]-q[j]))"
              "i > 0 and j > 0", "max(
        min(
            _c(ca, i-1, j, p, q),
            _c(ca, i-1, j-1, p, q),
            _c(ca, i, j-1, p, q)
        ),
        np.linalg.norm(p[i]-q[j])
        )"



              "-1": "ca[i, j]""}

Aucun commentaire:

Enregistrer un commentaire