0

私のプログラムは、array.fromfile() を使用してバイナリ ファイルから 420 万個の符号なし整数を配列 (パルス) に読み込みます。次に、これらの整数を繰り返し処理し、剰余演算に基づいて適切なリスト位置 (ゾーン) に追加します。現在、以下のループには約 2 秒かかりますが、もっと速くできることを願っています。私は何時間もかけてさまざまな numpy アプローチを試しましたが、そのうちのいくつかは遅くなりました。さまざまな機能をクロックしましたが、これが間違いなくボトルネックです。何か案は?

        for idx,val in enumerate(pulses):
            if (idx + 1)%nZones == 0 and idx != 0:
                zones[zone].append(val)            
                zone = 0
            else:
                zones[zone].append(val)
                zone += 1

例: 200 のゾーンがあり、パルス 1 がゾーン 1 に移動し、パルス 2 がゾーン 2 に移動し、200 番目のパルスに到達するまでパルス 201 がゾーン 1 に移動します。

4

4 に答える 4

1

np.reshape高速ですが、リストにはなりません。

いくつかのセットアップ:

N_ZONES = 200
MAX_VALUE = 100
N_VALUES = 4.2e6 + np.random.randint(N_ZONES) # Ensure a random number

# Random set of ~4.2 million values
pulses = np.random.randint(0, MAX_VALUE, (N_VALUES))

パルスの総数が 200 で割り切れるかわからないので、配列をパディングする必要があるかもしれません

# Pad pulses with negatives to have a size divisible by 200
PADDING = N_ZONES - pulses.shape[0] % N_ZONES
pulses_padded = np.hstack((pulses, np.zeros(PADDING)-1))

# Reshape
zones = pulses_padded.reshape((-1, N_ZONES)).T

最後の行にはPADDING、最後に負の値があります


その後、リストのリストに戻すことができますが、これは遅いです

# Convert back to lists (though this is slow)
zone_lists = [ [value for value in row if value >= 0] for row in zones]
于 2013-10-19T16:53:18.820 に答える
0

試す:

for idx,val in enumerate(pulses):
    zones[zone%nZones].append(val)
    zone+=1
于 2013-10-19T18:08:13.737 に答える
0

FWIW、これは純粋な Python バージョンです (テスト用に小さい値で表示します)。使用されるパルス値は、識別のために 1 から始まる連続値です。

from itertools import cycle, izip
from math import ceil

nPulses = 100
pulses = range(1, nPulses+1)
nZones = 20
nZoneSize = int( ceil(len(pulses) / float(nZones)) )
zones = [[None for _ in xrange(nZoneSize)] for z in xrange(1, nZones+1)]

for i, (p, z) in enumerate(izip(pulses, cycle(zones))):
    z[i / nZones] = p

for zone in zones:
    print zone

出力:

[1, 21, 41, 61, 81]  
[2, 22, 42, 62, 82]  
[3, 23, 43, 63, 83]  
[4, 24, 44, 64, 84]  
[5, 25, 45, 65, 85]  
[6, 26, 46, 66, 86]  
[7, 27, 47, 67, 87]  
[8, 28, 48, 68, 88]  
[9, 29, 49, 69, 89]  
[10, 30, 50, 70, 90] 
[11, 31, 51, 71, 91] 
[12, 32, 52, 72, 92] 
[13, 33, 53, 73, 93] 
[14, 34, 54, 74, 94] 
[15, 35, 55, 75, 95] 
[16, 36, 56, 76, 96] 
[17, 37, 57, 77, 97] 
[18, 38, 58, 78, 98] 
[19, 39, 59, 79, 99] 
[20, 40, 60, 80, 100]
于 2013-10-19T17:38:40.483 に答える