File: bench_strings.py

package info (click to toggle)
numpy 1%3A2.2.4%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 83,420 kB
  • sloc: python: 248,499; asm: 232,365; ansic: 216,874; cpp: 135,657; f90: 1,540; sh: 938; fortran: 558; makefile: 409; sed: 139; xml: 109; java: 92; perl: 79; cs: 54; javascript: 53; objc: 29; lex: 13; yacc: 9
file content (43 lines) | stat: -rw-r--r-- 1,233 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from .common import Benchmark

import numpy as np
import operator


_OPERATORS = {
    '==': operator.eq,
    '!=': operator.ne,
    '<': operator.lt,
    '<=': operator.le,
    '>': operator.gt,
    '>=': operator.ge,
}


class StringComparisons(Benchmark):
    # Basic string comparison speed tests
    params = [
        [100, 10000, (1000, 20)],
        ['U', 'S'],
        [True, False],
        ['==', '!=', '<', '<=', '>', '>=']]
    param_names = ['shape', 'dtype', 'contig', 'operator']
    int64 = np.dtype(np.int64)

    def setup(self, shape, dtype, contig, operator):
        self.arr = np.arange(np.prod(shape)).astype(dtype).reshape(shape)
        self.arr_identical = self.arr.copy()
        self.arr_different = self.arr[::-1].copy()

        if not contig:
            self.arr = self.arr[..., ::2]
            self.arr_identical = self.arr_identical[..., ::2]
            self.arr_different = self.arr_different[..., ::2]

        self.operator = _OPERATORS[operator]

    def time_compare_identical(self, shape, dtype, contig, operator):
        self.operator(self.arr, self.arr_identical)

    def time_compare_different(self, shape, dtype, contig, operator):
        self.operator(self.arr, self.arr_different)