Skip to content

Conversation

@lbillingham
Copy link
Owner

@lbillingham lbillingham commented Feb 21, 2022

This moves away from the shared itertools implimentations for finding pairs, triplets of the input numbers that sum to a given target.

Instead, we

  • 1st expose the underlying $~O^{dimensions}$ nested loops
  • trade some extra memory and some $O^{1}$ lookups to give us $~O^{dimensions-1}$
  • get a >= 170x speedup in our benchmarks

However, we:

  • loose the ability to properly work with floating point input
    • the fast lookup uses hasing and hashing floats gets weird due to floating point equality
  • can't trivially extend to higher-dimension problems: 4-element-tuples etc.

I've moved the float input tests out the their own file and away from the CI test path

Performance benchmarks

with these changes:

$ make benchmark
tests/performance_check.py ..                                                                                                                             [100%]

-------------------------------------------------------------------------------------------- benchmark: 2 tests --------------------------------------------------------------------------------------------
Name (time in us)               Min                   Max                  Mean              StdDev                Median                 IQR            Outliers          OPS            Rounds  Iterations
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_input1_pairs           22.1660 (1.0)        166.3790 (1.0)         23.6089 (1.0)        5.0170 (1.0)         23.0000 (1.0)        0.5123 (1.0)        87;521  42,356.8183 (1.0)        7677           1
test_input1_triplets     1,994.8000 (89.99)    3,561.1120 (21.40)    2,152.1272 (91.16)    204.1428 (40.69)    2,033.1040 (88.40)    299.1878 (584.07)       41;4     464.6565 (0.01)        341           1
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

itertools, but non-float supporting version

$ make benchmark
tests/performance_check.py ..                                                                                                      [100%]

------------------------------------------------------------------------------------- benchmark: 2 tests ------------------------------------------------------------------------------------
Name (time in ms)             Min                 Max                Mean            StdDev              Median               IQR            Outliers       OPS            Rounds  Iterations
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_input1_pairs          2.8727 (1.0)        4.2386 (1.0)        3.1265 (1.0)      0.1638 (1.0)        3.1067 (1.0)      0.1888 (1.0)          78;9  319.8414 (1.0)         326           1
test_input1_triplets     211.6325 (73.67)    213.3950 (50.35)    212.4042 (67.94)    0.6555 (4.00)     212.2717 (68.33)    0.8081 (4.28)          2;0    4.7080 (0.01)          5           1
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
=========================================================== 2 passed in 3.59s ============================================================

Note the change in units this branch is in microseconds, the itertools version is in milliseconds

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants