CLN: reduce overhead in setup for categoricals benchmarks in asv
State: closed

The setup functions for the `categoricals.CategoricalSlicing` suite have ~40s of overhead that can be eliminated by two approaches:

- Use `pd.CategoricalIndex.from_codes()` instead of `pd.CategoricalIndex()`
  - ~30s of the speedup comes from this
- Replace the dict of all cases with an `if`/`else` block to minimize unnecessary construction
  - Another ~10s speedup from this

Before:
```
$ time asv dev -b CategoricalSlicing
[...]
real    1m3.747s
user    0m46.250s
sys     0m15.141s
```

After:
```
$ time asv dev -b CategoricalSlicing
real    0m23.893s
user    0m14.578s
sys     0m6.578s
```

- [ ] closes #xxxx
- [ ] tests added / passed
- [ ] passes `git diff upstream/master -u -- "*.py" | flake8 --diff`
- [ ] whatsnew entry


Labels:
name: Clean
name: Benchmark
description: Performance (ASV) benchmarks

Comments:
Hello @qwhelan! Thanks for updating the PR.



Cheers ! There are no PEP8 issues in this Pull Request. :beers: 

##### Comment last updated on January 24, 2019 at 19:31 Hours UTC
# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24913?src=pr&el=h1) Report
> Merging [#24913](https://codecov.io/gh/pandas-dev/pandas/pull/24913?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/5761e359b65631db491349a65fc21d0da51dcc0f?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/24913/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/24913?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master   #24913   +/-   ##
=======================================
  Coverage   92.38%   92.38%           
=======================================
  Files         166      166           
  Lines       52404    52404           
=======================================
  Hits        48413    48413           
  Misses       3991     3991
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.8% <ø> (ø)` | :arrow_up: |
| #single | `42.89% <ø> (ø)` | :arrow_up: |


------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24913?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24913?src=pr&el=footer). Last update [5761e35...704ee8f](https://codecov.io/gh/pandas-dev/pandas/pull/24913?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

# [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24913?src=pr&el=h1) Report
> Merging [#24913](https://codecov.io/gh/pandas-dev/pandas/pull/24913?src=pr&el=desc) into [master](https://codecov.io/gh/pandas-dev/pandas/commit/5761e359b65631db491349a65fc21d0da51dcc0f?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/pandas-dev/pandas/pull/24913/graphs/tree.svg?width=650&token=eZ4WkYLtcO&height=150&src=pr)](https://codecov.io/gh/pandas-dev/pandas/pull/24913?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master   #24913   +/-   ##
=======================================
  Coverage   92.38%   92.38%           
=======================================
  Files         166      166           
  Lines       52404    52404           
=======================================
  Hits        48413    48413           
  Misses       3991     3991
```

| Flag | Coverage Δ | |
|---|---|---|
| #multiple | `90.8% <ø> (ø)` | :arrow_up: |
| #single | `42.89% <ø> (ø)` | :arrow_up: |


------

[Continue to review full report at Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24913?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/pandas-dev/pandas/pull/24913?src=pr&el=footer). Last update [5761e35...704ee8f](https://codecov.io/gh/pandas-dev/pandas/pull/24913?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

LGTM. Can you run `asv dev` for `CategoricalSlicing`?
@mroeschke Sure, here it is:
```
$ asv dev -b CategoricalSlicing
· Discovering benchmarks
· Running 5 total benchmarks (1 commits * 1 environments * 5 benchmarks)
[  0.00%] ·· Benchmarking existing-py_home_chris_anaconda3_bin_python
[ 10.00%] ··· categoricals.CategoricalSlicing.time_getitem_bool_array                                                                                                     ok
[ 10.00%] ··· ================ ==========
                   index
              ---------------- ----------
               monotonic_incr   3.06±0ms
               monotonic_decr   3.03±0ms
               non_monotonic    19.1±0ms
              ================ ==========

[ 20.00%] ··· categoricals.CategoricalSlicing.time_getitem_list                                                                                                           ok
[ 20.00%] ··· ================ ==========
                   index
              ---------------- ----------
               monotonic_incr   2.04±0ms
               monotonic_decr   1.26±0ms
               non_monotonic    854±0μs
              ================ ==========

[ 30.00%] ··· categoricals.CategoricalSlicing.time_getitem_list_like                                                                                                      ok
[ 30.00%] ··· ================ =========
                   index
              ---------------- ---------
               monotonic_incr   198±0μs
               monotonic_decr   145±0μs
               non_monotonic    200±0μs
              ================ =========

[ 40.00%] ··· categoricals.CategoricalSlicing.time_getitem_scalar                                                                                                         ok
[ 40.00%] ··· ================ =========
                   index
              ---------------- ---------
               monotonic_incr   438±0μs
               monotonic_decr   299±0μs
               non_monotonic    300±0μs
              ================ =========

[ 50.00%] ··· categoricals.CategoricalSlicing.time_getitem_slice                                                                                                          ok
[ 50.00%] ··· ================ =========
                   index
              ---------------- ---------
               monotonic_incr   186±0μs
               monotonic_decr   186±0μs
               non_monotonic    493±0μs
              ================ =========
```
thanks @qwhelan