phpexperts/combinatorics

处理组合的实用类集合。

v1.1.1 2021-03-31 12:36 UTC

This package is auto-updated.

Last update: 2024-08-29 04:49:07 UTC


README

TravisCI Maintainability Test Coverage

Combinatorics 是一个 PHP Experts, Inc. 项目,旨在简化组合算法的访问。

https://en.wikipedia.org/wiki/Combinatorics

内存消耗

存储成千上万的可能性可能会非常耗费内存。

然而,该项目利用 PHP 生成器,仅使用几千字节来计算和输出数百万种潜在组合(请参阅下方的 基准测试 部分)。

生成所有可能的组合

A、B、C 和 D 的所有可能性是什么?比你想象的要多!64 种。再加上两个字母,你就有了 1,956 种可能性。

在任意一天,8 辆车可以占据多少个不同的停车位顺序?在下面的 用法 部分找到答案!

安装

通过 Composer

composer require phpexperts/combinatorics

用法

$generator = new CombinationsGenerator();

foreach ($generator->generate($styles) as $combination) {
    // If you can do what you need to do with the combinations here, without immediately storing
    // them into an array, then your memory usage will never exceed the amount needed store one 
    // combination.
}

您可以在 Console Painter 项目 的测试套件中看到该项目的实际用法](https://github.com/PHPExpertsInc/ConsolePainter).

用例

测试

phpunit --testdox

为了真正了解正在发生的情况——以及运行压力测试,请运行

phpunit --debug

注意:在 Intel i7 上运行所有压力测试需要超过 5 分钟。

基准测试

Level 1: Generating combinations for A
[
  Number of possibilities => 2
  Time (ms)               => 0.051975250244141
  Time (s)                => 5.1975250244141E-5
  Memory consumed         => 0
  Peak Memory (Diff)      => 0
]
Level 2: Generating combinations for A, B
[
  Number of possibilities => 4
  Time (ms)               => 0.047922134399414
  Time (s)                => 4.7922134399414E-5
  Memory consumed         => 0
  Peak Memory (Diff)      => 0
]
Level 3: Generating combinations for A, B, C
[
  Number of possibilities => 15
  Time (ms)               => 0.22578239440918
  Time (s)                => 0.00022578239440918
  Memory consumed         => 0
  Peak Memory (Diff)      => 0
]
Level 4: Generating combinations for A, B, C, D
[
  Number of possibilities => 64
  Time (ms)               => 1.0910034179688
  Time (s)                => 0.0010910034179688
  Memory consumed         => 0
  Peak Memory (Diff)      => 0
]
Level 5: Generating combinations for A, B, C, D, E
[
  Number of possibilities => 325
  Time (ms)               => 6.5748691558838
  Time (s)                => 0.0065748691558838
  Memory consumed         => 0
  Peak Memory (Diff)      => 0
]
Level 6: Generating combinations for A, B, C, D, E, F
[
  Number of possibilities => 1956
  Time (ms)               => 47.721147537231
  Time (s)                => 0.047721147537231
  Memory consumed         => 0
  Peak Memory (Diff)      => 0
]
Level 7: Generating combinations for A, B, C, D, E, F, G
[
  Number of possibilities => 13699
  Time (ms)               => 395.29585838318
  Time (s)                => 0.39529585838318
  Memory consumed         => 0
  Peak Memory (Diff)      => 0
]
Level 8: Generating combinations for A, B, C, D, E, F, G, H
[
  Number of possibilities => 109600
  Time (ms)               => 3834.4430923462
  Time (s)                => 3.8344430923462
  Memory consumed         => 0
  Peak Memory (Diff)      => 0
]
Level 9: Generating combinations for A, B, C, D, E, F, G, H, I
[
  Number of possibilities => 986409
  Time (ms)               => 37884.353876114
  Time (s)                => 37.884353876114
  Memory consumed         => 320
  Peak Memory (Diff)      => 0
]
Level 10: Generating combinations for A, B, C, D, E, F, G, H, I, J
[
  Number of possibilities => 9864100
  Time (ms)               => 422606.03785515
  Time (s)                => 422.60603785515
  Memory consumed         => 0
  Peak Memory (Diff)      => 0
]

贡献者

Theodore R. Smith theodore@phpexperts.pro
GPG 指纹:4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690
首席执行官:PHP Experts, Inc.

{ "transactionId" : "$transactionId", "crmId" : "$crmId", "zAccountId" : "$zuoraAccountId", "facilityId" : "$facilityId", "promo_code" : "$promo_code", "transactionTotal" : "$total_cost_for_facebook", "transactionProducts" : [$transactionProducts], "transactionAffiliation" : "U.S. LawShield" }

许可协议

MIT 许可协议。有关更多信息,请参阅许可文件