Repository: numpy/numpy
File: benchmarks/benchmarks/bench_indexing.py (Python)

Code before:

    def time_assign(self, ndim):
        # time assignment from a python scalar
        arr = self.array
        indx = (1,) * ndim
        for i in range(100):
            arr[indx] = 5.

    def time_assign_cast(self, ndim):
        # time an assignment which may use a cast operation
        arr = self.array
        indx = (1,) * ndim
        val = np.int16(43)
        for i in range(100):
            arr[indx] = val


class BooleanAssignmentOrder(Benchmark):
    params = ['C', 'F']
    param_names = ['order']

    def setup(self, order):
        shape = (64, 64, 64)
        self.base = np.zeros(shape, dtype=np.uint32, order=order)
        # emulate gh-30156: boolean assignment into a sliced Fortran/C view
        self.view = self.base[1:-1, 1:-1, 1:-1]
        mask = np.random.RandomState(0).rand(*self.view.shape) > 0.5
        if order == 'F':
            mask = np.asfortranarray(mask)
        else:
            mask = np.ascontiguousarray(mask)
        self.mask = mask
        self.value = np.uint32(7)

    def time_boolean_assign_scalar(self, order):
        self.view[self.mask] = self.value


class IndexingSeparate(Benchmark):
    def setup(self):
        self.tmp_dir = mkdtemp()
        self.fp = memmap(pjoin(self.tmp_dir, 'tmp.dat'),
                         dtype=float32, mode='w+', shape=(50, 60))
        self.indexes = array([3, 4, 6, 10, 20])

    def teardown(self):
        del self.fp
        shutil.rmtree(self.tmp_dir)

    def time_mmap_slicing(self):
        for i in range(1000):

Review comment (suggestion):
Slicing shouldn't matter, I think, although, maybe we have a C-contig short-cut somewhere and then it would.

Code after:

    def time_assign(self, ndim):
        # time assignment from a python scalar
        arr = self.array
        indx = (1,) * ndim
        for i in range(100):
            arr[indx] = 5.

    def time_assign_cast(self, ndim):
        # time an assignment which may use a cast operation
        arr = self.array
        indx = (1,) * ndim
        val = np.int16(43)
        for i in range(100):
            arr[indx] = val


class BooleanAssignmentOrder(Benchmark):
    params = ['C', 'F']
    param_names = ['order']

    def setup(self, order):
        shape = (64, 64, 64)
        # emulate gh-30156: boolean assignment into a Fortran/C array
        self.base = np.zeros(shape, dtype=np.uint32, order=order)
        mask = np.random.RandomState(0).rand(*self.base.shape) > 0.5
        self.mask = mask.copy(order)
        self.value = np.uint32(7)

    def time_boolean_assign_scalar(self, order):
        self.base[self.mask] = self.value


class IndexingSeparate(Benchmark):
    def setup(self):
        self.tmp_dir = mkdtemp()
        self.fp = memmap(pjoin(self.tmp_dir, 'tmp.dat'),
                         dtype=float32, mode='w+', shape=(50, 60))
        self.indexes = array([3, 4, 6, 10, 20])

    def teardown(self):
        del self.fp
        shutil.rmtree(self.tmp_dir)

    def time_mmap_slicing(self):
        for i in range(1000):
            self.fp[5:10]

    def time_mmap_fancy_indexing(self):
        for i in range(1000):
            self.fp[self.indexes]