Efficient array operations in python

I am trying to create a fairly large array in python filled with zeros and ones. As a result, he should have about 1.2 billion. Records. I fill it, as in the example. The idea is that 400 entries are a time interval, and for each time interval there is a probability p that it is one. If so, it is filled with units for slot_duration time slots, otherwise it is filled with 400 inputs, one time slot, zeros.

import numpy as np

p = 0.01
slot_duration = 10
test_duration = 60
timeslots_left = test_duration * 1000 * 1000 / 20
transmission_array = []
while timeslots_left >= 0:
    rand_num = np.random.choice((0, 1), p=[1 - p, p])
    if rand_num == 1:
        for i in range(0, slot_duration):
            for j in range(0, 400):
                transmission_array.append(1)
        timeslots_left -= slot_duration
    else:
        for j in range(0, 400):
            transmission_array.append(0)
        timeslots_left -= 1

Performance is, of course, terrible. Within 10 seconds, it takes about 45 seconds to generate the array, but it also takes 45 seconds to iterate over.

: ? , , ? , ?

.

+4
2

, :

    for i in range(0, slot_duration):
        for j in range(0, 400):
            transmission_array.append(1)

transmission_array.extend([1]*400*slot_duration)

1 , -, 1 . , / .

slot_duration , :

chunk = [1]*400*slot_duration

, transmission_array.extend(chunk)

chunk

, :

    for j in range(0, 400):
        transmission_array.append(0)

    transmission_array.extend(zero_array)

zero_array = [0]*400

+1

pythonic.

, .

p = 0.01
slot_duration = 10
test_duration = 60
timeslots_left = test_duration * 1000 * 1000 / 20
transmission_array = []
while timeslots_left >= 0:
    rand_num = np.random.choice((0, 1), p=[1 - p, p])
    duration = slot_duration if rand_num == 1 else 1
    transmission_array.extend([rand_num] * 400 * duration)
    timeslots_left -= duration

, . .

+1

Source: https://habr.com/ru/post/1668361/


All Articles