Skip to content

Instantly share code, notes, and snippets.

@belltailjp
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belltailjp/d40a214321471be4e329 to your computer and use it in GitHub Desktop.
Save belltailjp/d40a214321471be4e329 to your computer and use it in GitHub Desktop.
Benchmark of enforcing 1st row/col to be smaller than the second.
import numpy
from benchmarker import Benchmarker
N = 10 ** 8
with Benchmarker(1000) as bench:
@bench('col-proposed')
def _(bm):
a = numpy.random.randint(0, 100, (2, N))
with bm:
a[:, a[1] < a[0]] = a[:, a[1] < a[0]][::-1]
@bench('row-proposed')
def _(bm):
a = numpy.random.randint(0, 100, (N, 2))
with bm:
a[a[:, 1] < a[:, 0]] = a[a[:, 1] < a[:, 0]][:, ::-1]
@bench('col-conventional')
def _(bm):
a = numpy.random.randint(0, 100, (2, N))
with bm:
for j in range(a.shape[1]):
if a[1, j] < a[0, j]:
a[1, j], a[0, j] = a[0, j], a[1, j]
@bench('row-conventional')
def _(bm):
a = numpy.random.randint(0, 100, (N, 2))
with bm:
for i in range(a.shape[0]):
if a[i, 1] < a[i, 0]:
a[i, 1], a[i, 0] = a[i, 0], a[i, 1]
@belltailjp
Copy link
Author

Here is an execution result.

## benchmarker:         release 4.0.1 (for python)
## python version:      3.4.3
## python compiler:     GCC 4.8.2
## python platform:     Linux-3.13.0-24-generic-x86_64-with-debian-jessie-sid
## python executable:   /home/#####/.pyenv/versions/3.4.3/bin/python
## cpu model:           Intel(R) Core(TM) i5-3570 CPU @ 3.40GHz  # 3401.000 MHz
## parameters:          loop=1000, cycle=1, extra=0

##                                       real    (total    = user    + sys)
col-proposed                           3.6327    3.6200    2.8100    0.8100
row-proposed                           3.6977    3.6900    2.8700    0.8200
col-conventional                      49.9127   49.8600   49.7600    0.1000
row-conventional                      51.3075   51.2600   51.1600    0.1000

## Ranking                               real
col-proposed                           3.6327  (100.0) ********************
row-proposed                           3.6977  ( 98.2) ********************
col-conventional                      49.9127  (  7.3) *
row-conventional                      51.3075  (  7.1) *

## Matrix                                real    [01]    [02]    [03]    [04]
[01] col-proposed                      3.6327   100.0   101.8  1374.0  1412.4
[02] row-proposed                      3.6977    98.2   100.0  1349.8  1387.6
[03] col-conventional                 49.9127     7.3     7.4   100.0   102.8
[04] row-conventional                 51.3075     7.1     7.2    97.3   100.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment