Search code examples
pythonpython-3.xnumpydivision

Faster division in python


I have this lambda-function:

transfer = lambda x: 1.0 / (1.0 + np.e ** (-2.0*x))

This function is called very often (more than 80 000 times). (I think division 1 by the expression is not effective, but cannot find good alternative for that.) Can I make this faster?


Solution

  • Rewrite your code such that you can collect all x values in a numpy array first and then use one operation on your numpy array directly

    import time
    import numpy as np
    
    data = np.random.random(size=100000)
    
    result = list()
    
    transfer = lambda x: 1.0 / (1.0 + np.e ** (-2.0 * x))
    
    t1 = time.perf_counter()
    for value in data:
        y = transfer(value)
        result.append(y)
    result = np.array(result)
    t2 = time.perf_counter()
    print(f"time lambda = {t2 - t1} with sum {result.sum()}")
    
    t3 = time.perf_counter()
    
    # this line is the numpy expression of your transfer function
    results2 = 1 / (1 + np.e ** (-2 * data))
    t4 = time.perf_counter()
    print(f"time numpy = {t4 - t3} with sum {results2.sum()}")
    

    Output of this code is:

    time lambda = 0.09890314500080422 with sum 71682.58495450807
    time numpy = 0.0029907969874329865 with sum 71682.58495450807
    

    So the numpy approach is about 35 times faster