Here we test and check the performance of the array_arange implementation.


In [1]:
from __future__ import print_function
from functools import partial
from itertools import izip as zip, imap as map
from numpy import arange
import numpy as np
onesi = partial(np.ones, dtype=np.int32)
cumsumi = partial(np.cumsum, dtype=np.int32)
%matplotlib inline

In [2]:
def aarng(start, end):
    return np.hstack(map(arange, start, end))

In [3]:
def aarng_for_list(start, end):
    return np.hstack([arange(s, e) for s, e in zip(start, end)])

In [4]:
def aarng_for_gen(start, end):
    return np.hstack((arange(s, e) for s, e in zip(start, end)))

In [5]:
def aarng_concat(start, end):
    return np.concatenate(list(map(arange, start, end)))

In [6]:
def aarng_concat_list(start, end):
    return np.concatenate([arange(s, e) for s, e in zip(start, end)])

In [7]:
def aarng_concat_tuple(start, end):
    return np.concatenate(tuple(map(arange, start, end)))

In [19]:
def aarng_one(start, end):
    n = end - start
    arr = onesi(n.sum())
    # set pointers correct for cumsum
    ptr = cumsumi(n[:-1])
    arr[0] = start[0]
    arr[ptr] = start[1:]
    # Correct for previous values
    arr[ptr] -= start[:-1] + n[:-1] - 1
    return cumsumi(arr)


[ 3  4  5  8 50 51 52 53 80 81]
[ 3  4  5  8 50 51 52 53 80 81]

In [21]:
# Samples
N = 200000
start = np.random.randint(1000, size=N)
end = start + np.random.randint(10, size=N) + start

In [22]:
%timeit np.array((aarng(start, end)))


1 loop, best of 3: 1.17 s per loop

In [23]:
%timeit np.array(aarng_for_list(start, end))


1 loop, best of 3: 1.15 s per loop

In [24]:
%timeit np.array(aarng_for_gen(start, end))


1 loop, best of 3: 1.21 s per loop

In [25]:
%timeit np.array(aarng_concat(start, end))


1 loop, best of 3: 992 ms per loop

In [26]:
%timeit np.array(aarng_concat_list(start, end))


1 loop, best of 3: 985 ms per loop

In [27]:
%timeit np.array(aarng_concat_tuple(start, end))


1 loop, best of 3: 989 ms per loop

In [28]:
%timeit np.array(aarng_one(start, end))


1 loop, best of 3: 550 ms per loop

In [ ]: