No speed gains from Cython

后端 未结 7 769
不思量自难忘°
不思量自难忘° 2021-01-30 14:05

I am trying to define a function that contains an inner loop for simulating an integral.

The problem is speed. Evaluating the function once can take up to 30 seconds on

相关标签:
7条回答
  • 2021-01-30 14:57

    Splitting data only, after your comment on 28 Nov:

    import sys
    import time
    import numpy as np
    
    def splitdata( data, n, start=1971 ):
        """ split data into n pieces, where col 1 == start .. start + n """
            # not fancy, fast enough for small n
        split = n * [None]
        for j in range(n):
            split[j] = data[ data[:,1] == start + j ]
        return split  # [ arrays: col1 0, col1 1 ... ]
    
    #...........................................................................
    N = 2237
    ncol = 21
    start = 1971
    n = 20
    seed = 1
    exec "\n".join( sys.argv[1:] )  # run this.py N= ...
    np.set_printoptions( 2, threshold=100, suppress=True )  # .2f
    np.random.seed(seed)
    
    print "N=%d  ncol=%d  n=%d" % (N, ncol, n)
    data = np.random.uniform( start, start + n, (N,ncol) )
    data[:,1] = data[:,1].round()
    t0 = time.time()
    
    split = splitdata( data, n, start )  # n pieces
    
    print "time: %.0f us splitdata" % ((time.time() - t0) * 1e6)
    for y, yeardata in enumerate(split):
        print "%d  %d  %g" % (start + y, len(yeardata), yeardata[:,0].sum())
    

    -->

    time: 27632 us splitdata  # old mac ppc
    1971  69  136638
    1972  138  273292
    ...
    
    0 讨论(0)
提交回复
热议问题