As Sebastian said, it's going to take more information about context. It's very unlikely that you actually need these particular arrays-- probably, you're interested in some more important result which you're using these arrays as an intermediate step to obtain. Their structure makes that even more likely than usual.
Without knowing how they're being used, no one can know whether they should be replaced by a nested numba or cython loop, or a clever use of einsum, or who knows what else. The minimum time code can take is no time. :-) Doug On Sun, 10 Jan 2021 at 02:00, <klark--k...@yandex.ru> wrote: > Thank you but the same time: > import numpy as np > import time > N = 3000 > m_0 = np.arange(N) > a = np.ones(N) > #===== first ========= > t1 = time.time() > m_1 = np.outer(m_0, a).ravel() > m_2 = np.outer(a, m_0).ravel() > t1 = time.time() - t1 > #===== second ======= > t2 = time.time() > m_3 = np.tile(m_0, N) > m_4 = np.repeat(m_0, N) > t2 = time.time() - t2 > #================== > np.sum(m_1 - m_3) > np.sum(m_2 - m_4) > t1 > t2 > > 10.01.2021, 08:37, "V. Armando Sole" <s...@esrf.fr>: > > I guess the speed up, if any, will be machine dependent, but you can give > a try at: > > import numpy as np > import time > N = 3000 > m_0 = np.arange(N) > t = time.time() > a = np.ones(N) > m_1 = np.outer(m_0, a).ravel() > m_2 = np.outer(a, m_0).ravel() > t = time.time() - t > > > > > On 2021-01-09 20:07, klark--k...@yandex.ru wrote: > > Hello. There is a random 1D array m_0 with size 3000, for example: > > m_0 = np.array([0, 1, 2]) > > I need to generate two 1D arrays: > > m_1 = np.array([0, 1, 2, 0, 1, 2, 0, 1, 2]) > m_2 = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2]) > > Is there faster way to do it than this one: > > import numpy as npimport time > N = 3 > m_0 = np.arange(N) > > t = time.time() > m_1 = np.tile(m_0, N) > m_2 = np.repeat(m_0, N) > t = time.time() - t > > I tried other ways but they are slower or have the same time. Other NumPy > operations in my code 10-100 times faster. Why the repeating an array is so > slow? I need 10 times speed up. Thank you for your attantion to my problem. > > > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion > > , > > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion > > _______________________________________________ > NumPy-Discussion mailing list > NumPy-Discussion@python.org > https://mail.python.org/mailman/listinfo/numpy-discussion >
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@python.org https://mail.python.org/mailman/listinfo/numpy-discussion