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

#渋谷java : Java の数学関数を計算速度的に極めたい

#渋谷java : Java の数学関数を計算速度的に極めたい

第十五回 #渋谷java http://shibuya-java.connpass.com/event/29113/ での発表資料です。

E77287648aff5484ac7659748e45c936?s=128

KOMIYA Atsushi

April 23, 2016
Tweet

Transcript

  1. Java ͷ਺ֶؔ਺Λ ܭࢉ଎౓తʹۃΊ͍ͨ ौ୩ Java 15th, 2016-04-23 KOMIYA Atsushi

  2. ͓·͑ͩΕΑ

  3. KOMIYA Atsushi @komiya_atsushi

  4. None
  5. Spark ຊΛॻ͖·ͨ͠ͷͰ ΑΖ͚͠Ε͹ͥͻ͝ߪೖ͍ͩ͘͞ʂ

  6. ࠓ೔ͷτϐοΫ

  7. Java ͷ਺ֶؔ਺

  8. java.lang.Math ͔Β࢝Ίͯ Java ͷ਺ֶؔ਺ͷܭࢉ଎౓Λ ௥ٻ͍͖ͯ͠·͢

  9. Implementations of Math functions in JDK

  10. java.lang.Math & java.lang.StrictMath

  11. JDK ඪ४ͷ਺ֶؔ਺ • ਺ֶؔ਺ͷ࣮૷ͱͯ͠ java.lang.Math ͱ java.lang.StrictMath ͕ఏڙ͞Ε͍ͯΔ • ͦΕͧΕԿ͕ҧ͏ͷ͔ʁ

  12. java.lang.StrictMath • https://docs.oracle.com/javase/jp/8/docs/api/java/lang/ StrictMath.html ΑΓ • ʮJavaϓϩάϥϜͷҠ২ੑΛอͭͨΊʹɺ͜ͷύοέʔδʹ͋ΔҰ ෦ͷ਺஋ؔ਺ͷఆٛ͸ɺطଘͷΞϧΰϦζϜͱಉҰͷܭࢉ݁ՌΛग़ ͢͜ͱ͕ٻΊΒΕ͍ͯ·͢ɻ͜͏ͨ͠ΞϧΰϦζϜ͸ɺ༗໊ͳωο τϫʔΫɾϥΠϒϥϦͰ͋Δnetlib͔ΒʮFreely

    Distributable Math Libraryʯ(fdlibm)ύοέʔδͱͯ͠ೖखՄೳͰ͢ɻ͜ΕΒͷΞϧΰ ϦζϜ͸CݴޠͰهड़͞Ε͓ͯΓɺ͢΂ͯͷුಈখ਺఺ԋࢉ͕Java ͷුಈখ਺఺ԋࢉϧʔϧʹै࣮ͬͯߦ͞ΕΔ΋ͷͱݟͳ͞Ε· ͢ɻʯ
  13. java.lang.StrictMath • ҟͳΔϓϥοτϑΥʔϜ (OS, CPU ͳͲ) ʹ
 ͓͍ͯɺϏοτύλʔϯͷϨϕϧͰಉ݁͡Ռ ͕ฦ͞ΕΔ͜ͱ (࠶ݱੑ)

    ͕อূ͞Ε͍ͯΔ • ଎౓ੑೳ͸ೋͷ࣍
  14. java.lang.Math • https://docs.oracle.com/javase/jp/8/docs/api/ java/lang/Math.html ΑΓ • ʮStrictMathΫϥεͷҰ෦ͷ਺஋ϝιουͱ͸ҟͳ ΓɺMathΫϥεͷ౳Ձؔ਺ͷ͢΂ͯͷ࣮૷͸ɺ ϏοτରϏοτͷಉ݁͡ՌΛฦ͢Α͏ʹ͸ఆٛ͞Ε ͍ͯ·ͤΜɻ͜ͷΏΔ΍͔͞ʹΑͬͯɺݫີͳ࠶ݱ

    ੑ͕ཁٻ͞Εͳ͍࣮૷ʹ͓͍ͯύϑΥʔϚϯεͷ ޲্͕ՄೳʹͳΓ·͢ɻʯ
  15. java.lang.Math • CPU ʹґଘ໋ͨ͠ྩͳͲ΋ར༻͠ͳ͕Βɺ
 ΑΓΑ͍଎౓ੑೳΛఏڙ͢Δ • ͨͩ͠ʮਖ਼֬͞ʯΛ٘ਜ਼ʹ͢ΔΘ͚Ͱ͸ͳ͍ • ࣮૷࢓༷తʹ͸ʮਖ਼֬͞ʯʮ୯ௐੑʯ͕ཁٻ ͞Ε͍ͯΔ

  16. Alternatives of java.lang.Math

  17. commons-math3 org.apache.commons:commons-math3:3.6.1

  18. commons-math3 • FastMath Ϋϥε͕ఏڙ͞Ε͍ͯΔ • java.lang.Math ͱಉ͡ϝιουߏ੒ • ΑΓ଎͘ɺ͔ͭਫ਼౓͕ߴ͍͜ͱΛᨳ͍ͬͯΔ •

    JIT ʹΑΔ࠷దԽΛલఏͱ͠ɺ·ͨେ͖Ίͷ ϧοΫΞοϓςʔϒϧΛར༻ͯ͠ߴ଎ԽΛਤͬ ͍ͯΔ
  19. jafama net.jafama:jafama:2.1.0

  20. jafama • ͪ͜Β΋ FastMath Ϋϥε͕ఏڙ͞Ε͍ͯΔ • 1e-15 (0.000000000000001) ͷਫ਼౓Β͍͠ •

    JIT ʹΑΔ࠷దԽΛલఏͱ͍ͯ͠Δ • ਫ਼౓Λ٘ਜ਼ʹͨ͠ɺ਺ֶؔ਺ͷߴ଎࣮૷΋ఏڙ͞ Ε͍ͯΔ • xxxQuick() ͷΑ͏ʹɺ”Quick” ͷαϑΟοΫε͕ ෇͍͍ͯΔ
  21. Comparison: performance

  22. ܭࢉ଎౓ͷൺֱ • java.lang.Math ͱ commons-math3, jafama ͱΛൺֱ͢Δ • jmh ͰεϧʔϓοτΛܭଌ͢Δ

    • ࣍ͷ਺ֶؔ਺ͦΕͧΕʹ͍ͭͯܭଌ͢Δ • log, log1p, exp, tanh, pow, sqrt • ػցֶशͷ࣮૷ʹͯΑ͘ར༻͢Δؔ਺܈Ͱ͢ • ਺ֶؔ਺ʹ͸ɺεέʔϧͷҟͳΔҾ਺Λෳ਺༻ҙ͢Δ • ಛఆͷ਺஋ (۠ؒ) ʹରԠͨ͠࠷దԽ࣮૷͕͞Ε͍ͯΔ৔߹ ͕͋ΔͨΊ
  23. ϕϯνϚʔΫϓϩάϥϜ https://github.com/komiya-atsushi/ java-playground/tree/master/math- functions

  24. log

  25. log • commons-math3 ͕಄ͻͱͭൈ͖Μग़͍ͯΔ • 1.234 ͷҾ਺Λ༩͑ͨͱ͖ͷੑೳ͕ҟৗ஋త ʹඈͼൈ͚͍ͯΔ͚Ͳɺ͜Ε͸ΘΓͱී௨ Ͱ͢ •

    jafama ͱ jdk ͸΄΅ಉ͡ੑೳ
  26. log1p

  27. log1p • log ͱ͸ҟͳΓɺjafama ͕ѹ౗తʹΑ͍ • jdk ͱ commons-math3 ͸େࠩ͸ͳ͍͕ɺ


    jdk > commons-math3 Ͱ͋Δ • commons-math3 ͷ log ͷ݁Ռ͸Կͩͬͨ ͷ͔…
  28. exp

  29. exp • commons-math3, jafama ͱ΋ʹɺҾ਺͕େ͖ ͘ͳΔʹͭΕͯੑೳ͕ߴ·Δ • ಛʹ jafama ͸શൠతʹੑೳ͕Α͍

    • ҰํͰ jdk ͸…
  30. tanh

  31. tanh • ͍ͣΕͷ࣮૷΋ɺҾ਺ͷεέʔϧ͕େ͖͘ͳ Δͱɺੑೳ͕Α͘ͳΔ • jafama > commons-math3 > jdk

  32. sqrt

  33. sqrt • େࠩͳ͍ • ͲΕΛ࢖ͬͯ΋΄ͱΜͲಉ͡

  34. pow

  35. pow • jafama ͸ϐʔΩʔͰ͸͋Δ͕ɺશମతʹ༏Ε͍ͯ Δ • jdk ΋ϐʔΩʔա͗Δ • power

    ͕খ͍͞৔߹͸ commons-math3 ΑΓ΋ Α͍ੑೳͰ͸͋Δ • commons-math3 ͸ຌ༱
  36. Comparison: accuracy

  37. ܭࢉਫ਼౓ͷൺֱ • java.lang.StrictMath ʹ͓͚Δ࣮૷ͷ໭Γ஋ͱൺֱ͢Δ • (StrictMath ͷ஋͕ਖ਼͍͠Θ͚Ͱ΋ͳ͍ͷ͕ͩ…) • ໭Γ஋ಉ࢜ͷࠩ෼ʹର͠ɺStrictMath ଆͷ໭Γ஋Λ

    ෼฼ͱׂͯ͠߹Λܭࢉ͢Δ • ͦͷׂ߹ͷฏۉઈର஋ͱ࠷େ஋Ͱਫ਼౓ΛݟͯΈΔ • Ҿ਺ͷ஋͸ݻఆͤͣɺཚ਺ੜ੒ͨ͠΋ͷΛ༩͑Δ
  38. Result: average DPNNPOTNBUI KBGBNB MPH & & MPHQ & &

    FYQ & & UBOI &  &  TRSU &  & 
  39. Result: maximum DPNNPOTNBUI KBGBNB MPH & & MPHQ & &

    FYQ & & UBOI &  &  TRSU &  & 
  40. Result • ͍ͣΕʹ͓͍ͯ΋ɺେ͖ͳࠩҟ͕͋ΔΘ͚Ͱ ͸ͳ͍ • 1e-15 ͷਫ਼౓͸อূͰ͖ͦ͏ • jafama ʹ͍ͭͯ͸ɺStrictMath

    ͷ໭Γ஋ͱҟ ͳΔ͜ͱ͕ଟ͍܏޲ʹ͋Γͦ͏
  41. Conclusion

  42. ·ͱΊ • ʮۜͷ஄ؙ͸ଘࡏ͠ͳ͔ͬͨʯ • “FastMath” ͱ໊৐͍ͬͯͯ΋ɺ͢΂ͯͷ਺ֶؔ਺ ͷ࣮૷ʹ͓͍ͯ JDK ͷ࣮૷ΑΓ΋༏Ε͍ͯΔɺͱ ͍͏Θ͚Ͱ͸ͳ͍

    • ·ͨɺ਺ֶؔ਺ʹ༩͑ΔҾ਺࣍ୈͰੑೳ͕େ͖͘ҟΔ ͜ͱ΋͋Γ͏Δ • ϢʔεέʔεʹԠͯ͡ɺ֤छ࣮૷Λ࢖͍෼͚Δ͜ͱ͕ ඞཁ
  43. Thank you!