Upgrade to Pro — share decks privately, control downloads, hide ads and more …

前回の”どう書く”を もっと高速にしてみた 2019.4.17 Nagoya.php #15

前回の”どう書く”を もっと高速にしてみた 2019.4.17 Nagoya.php #15

inaka-phper

April 18, 2019
Tweet

More Decks by inaka-phper

Other Decks in Technology

Transcript

  1. લճͷ”Ͳ͏ॻ͘”Λ
    ΋ͬͱߴ଎ʹͯ͠Έͨ
    2019.4.17 Nagoya.php #15

    View full-size slide

  2. ·ͣ͸ࣗݾ঺հ

    View full-size slide

  3. ͱ͋Δాࣷͷphper
    @inaka_phper
    phpྺ16೥(࣮ྗ͕൐͍ͬͯΔ͔͸ผ)
    3೥લʹ۽ຊ͔Β໊ݹ԰΁స৬͖ͯ͠·ͨ͠ɻ
    4ࡀࣇͱ1ࡀࣇͷ෕

    ՈͰͷझຯ։ൃ͢Δ࣌ؒΛͲ͏೧ग़͢Δ͔͕࠷ۙͷ೰Έ

    View full-size slide

  4. લճͷ໰୊Λ͓͞Β͍

    View full-size slide

  5. IUUQOBCFUBOJTBLVSBOFKQIFOBPSEFQBMJO

    View full-size slide

  6. ૉ௚ʹղ͍ͯ΋…
    public function run(string $input): int
    {
    $args = explode(",", $input);
    $start = (int)$args[0];
    $end = (int)$args[1];
    $tobase = (int)$args[2];
    $visibleCounter = 0;
    for ($i = $start; $i < $end; $i++) {
    $number = base_convert($i, 10, $tobase);
    if ($number === strrev($number)) {
    $visibleCounter++;
    }
    }
    return $visibleCounter;
    }

    View full-size slide

  7. λΠϜΞ΢τ
    phper:NagoyaPhp14 phper$ composer test
    > vendor/bin/phpunit
    PHPUnit 7.5.7 by Sebastian Bergmann and contributors.
    ......................
    [Symfony\Component\Process\Exception\ProcessTimedOutException]
    The process "vendor/bin/phpunit" exceeded the timeout of 300 seconds.

    View full-size slide

  8. λΠϜΞ΢τͷݪҼ
    ['12345,987654321,2', 62684],
    ['2467130,8433468,2', 2902],
    ['323901,4712975,10', 4389],
    ['12345,987654321,36', 67446],
    ['3969344,4086910,24', 205],
    ['19743263,83912295,5', 11553],
    ['6349529,39870823,10', 6637],
    ['66160071,153732445,5', 5605],
    ['18799557,189007582,14', 33741],
    ['78547566,225312226,20', 18346],
    ['143084571,506549072,18', 62323],
    ['2099642384,2789567569,6', 14787],
    ࢦఆͷςετέʔεͰ͸َϧʔϓ͕ൃੜ͢Δ

    View full-size slide

  9. ߴ଎Խ͢Δʹ͸
    ϧʔϓΛݶք·ͰݮΒ͢͜ͱ

    View full-size slide

  10. IUUQTTQFBLFSEFDLDPNUUTLDIKJOSJGBMTFUPVTIVLVXFOUJGBMTFBSVLPSJTVNVMJ TMJEF
    Ҏ߱ɺ్த·Ͱ͖ͨͭͪ͞ΜͱҰॹ

    View full-size slide

  11. લճɺ୭΋͕ࢥͬͨ͸ͣ…
    ܭࢉͰग़དྷΔͷͰ͸ʁ

    View full-size slide

  12. ݕূͨ݁͠Ռ

    View full-size slide

  13. ܭࢉͰ͍͚·ͨ͠ɻ

    View full-size slide

  14. ςετέʔε݁Ռ
    phper:NagoyaPhp14 phper$ composer test
    > vendor/bin/phpunit
    PHPUnit 7.5.7 by Sebastian Bergmann and contributors.
    .................................. 34 / 34 (100%)
    Time: 47 ms, Memory: 4.00 MB
    OK (34 tests, 34 assertions)
    phper:NagoyaPhp14 phper$

    View full-size slide

  15. υϠΝʂʂ
    phper:NagoyaPhp14 phper$ composer test
    > vendor/bin/phpunit
    PHPUnit 7.5.7 by Sebastian Bergmann and contributors.
    .................................. 34 / 34 (100%)
    Time: 47 ms, Memory: 4.00 MB
    OK (34 tests, 34 assertions)
    phper:NagoyaPhp14 phper$

    View full-size slide

  16. Ͳ͏ܭࢉͨ͠ͷ͔ʁ

    View full-size slide

  17. 10000-99999ͰճจͱͳΔ਺஋
    Λग़ྗͯ͠๏ଇΛ୳Δ
    (10ਐ਺)

    View full-size slide

  18. 10000-99999ͰճจΛग़ྗ
    10101
    10201
    10301
    10401
    10501
    10601
    10701
    10801
    10901
    11011
    11111
    11211
    11311
    11411
    11511
    11611
    11711
    11811
    11911
    12021

    99099
    99199
    99299
    99399
    99499
    99599
    99699
    99799
    99899

    View full-size slide

  19. 10000-99999ͰճจΛग़ྗ
    10101
    10201
    10301
    10401
    10501
    10601
    10701
    10801
    10901
    11011
    11111
    11211
    11311
    11411
    11511
    11611
    11711
    11811
    11911
    12021

    99099
    99199
    99299
    99399
    99499
    99599
    99699
    99799
    99899
    ࠨ൒෼ͷ਺஋͕ͭͣͭ૿͑ͯΔ

    View full-size slide

  20. ࠷ޙ - ։࢝ = ճจग़ݱ਺

    View full-size slide

  21. ࠷ޙ 99999
    ࠷ॳ 10000
    999 - 100 = 899

    View full-size slide

  22. ߟྀ͢΂͖έʔε

    View full-size slide

  23. ૝ఆ͞ΕΔߟྀ͠ͳ͍ͱ͍͚
    ͳ͍ࣄ
    1. php͸nਐ਺ͷ··ܭࢉͰ͖ͳ͍
    2. ӈ൒෼ͷ։࢝ɾ࠷ޙͷҐஔʹΑͬͯޡ͕ࠩ
    ग़Δ
    3. ح਺͔Βۮ਺΁ͷ܁Γ্͕Γͷࠨ൒෼…

    View full-size slide

  24. php ͸nਐ਺ͷ··ܭࢉͰ͖
    ͳ͍
    ໘౗͕ͩม׵͠௚ͯ͠ճආ
    1. ࢦఆͷnਐ਺΁ม׵
    2. ࠨ൒෼Λ੾Γग़͢
    3. ੾Γग़ͨ͠ࠨ൒෼Λ10ਐ਺΁
    255 -> ff
    ff -> f
    f -> 15

    View full-size slide

  25. ӈ൒෼ͷ։࢝ɾ࠷ޙͷҐஔʹ
    Αͬͯޡ͕ࠩग़Δ
    10000ͷࠨ൒෼
    10099ͷࠨ൒෼
    ಉ͡100͕ͩޙऀ͸10001͕੒ཱ͠ͳ͍
    ࠷ॳͱ࠷ޙ͸ࠨ൒෼ͷճจͷൣғ಺͔νΣοΫ

    View full-size slide

  26. ح਺͔Βۮ਺΁ͷ܁Γ্͕Γ
    ͷࠨ൒෼…
    10000

    99999
    100000
    ܁Γ্͕ͬͨͷʹࠨ൒෼͚ͩͰݟΔͱॳظ஋ʹ໭ͬͯ͠·͏
    ͜ͷλΠϛϯάͰҰ୴ 999 - 100Λͯ݁͠ՌΛੵΈ্͛ͯճආ

    View full-size slide

  27. ͜ΕΒͷରࡦͰ
    όονϦͰͨ͠

    View full-size slide