Slide 1

Slide 1 text

Takanori Hirobe Grand Central DispatchʹΑΔ
 ฒߦΞϧΰϦζϜೖ໳ iOSDC JAPAN 2020

Slide 2

Slide 2 text

ࣗݾ঺հ • ኍ෦ وಙ( Takanori Hirobe ) • Twitter, Github, Qiita: @taka1068

Slide 3

Slide 3 text

ࣗݾ঺հ • ኍ෦ وಙ( Takanori Hirobe ) • Twitter, Github, Qiita: @taka1068 • ϑΣϯϦϧגࣜձࣾॴଐ

Slide 4

Slide 4 text

ࣗݾ঺հ • ኍ෦ وಙ( Takanori Hirobe ) • Twitter, Github, Qiita: @taka1068 • ϑΣϯϦϧגࣜձࣾॴଐ ϓϥνφεϙϯαʔ

Slide 5

Slide 5 text

ࢿྉʹ͍ͭͯ ϋογϡλά෇͖ͰπΠʔτ͠·͢ • εϥΠυpdf • https://speakerdeck.com/taka1068/concurrencywithgcd
 
 
 • શαϯϓϧίʔυ • https://github.com/taka1068/iOSDC-2020-demo

Slide 6

Slide 6 text

ΞδΣϯμ • ࠷ۙͷiOSσόΠεͱϚϧνίΞԽʹؔ͢Δ՝୊ • for loopΛฒߦԽ͢Δ • ΫΠοΫιʔτΛฒߦԽ͢Δ • ͍Ζ͍ΖͳΦʔόʔϔου

Slide 7

Slide 7 text

ΞδΣϯμ • ࠷ۙͷiOSσόΠεͱϚϧνίΞԽʹؔ͢Δ՝୊ • for loopΛฒߦԽ͢Δ • ΫΠοΫιʔτΛฒߦԽ͢Δ • ͍Ζ͍ΖͳΦʔόʔϔου

Slide 8

Slide 8 text

࠷ۙͷiOSσόΠεͱϚϧνίΞ • iPhone 11 • 6 ίΞ • iPad Pro • 8 ίΞ

Slide 9

Slide 9 text

࠷ۙͷiOSσόΠεͱϚϧνίΞ • iPhone 11 • 6 ίΞ • iPad Pro • 8 ίΞ • Mac Pro • ࠷େ28 ίΞ

Slide 10

Slide 10 text

• ࠓޙ΋ϚϧνίΞԽ͕ਐΉ͜ͱ͸࣮֬ • iOSΤϯδχΞʹͱͬͯɺϚϧνίΞΛ׆͔͢஌ݟ΋ॏཁʹͳͬͯ͘Δ͸ͣ

Slide 11

Slide 11 text

• ࠓޙ΋ϚϧνίΞԽ͕ਐΉ͜ͱ͸࣮֬ • iOSΤϯδχΞʹͱͬͯɺϚϧνίΞΛ׆͔͢஌ݟ΋ॏཁʹͳͬͯ͘Δ͸ͣ

Slide 12

Slide 12 text

Grand Central Dispatch

Slide 13

Slide 13 text

Grand Central Dispatch • Apple७ਖ਼ͷϑϨʔϜϫʔΫ • Swift΍Xcodeͱͷ਌࿨ੑ͕ߴ͍ • watchOS͔ΒmacOS·Ͱ࢖͑Δ • pthreadͳͲͱൺ΂Δͱ࢖͍΍͍͢

Slide 14

Slide 14 text

DispatchQueue.main.async {} ͔͠࢖ͬͨ͜ͱͳ͍ ϚϧνεϨου͸ͳΜͱͳۤ͘ख

Slide 15

Slide 15 text

Grand Central Dispatchʹ਌͠΋͏

Slide 16

Slide 16 text

Grand Central Dispatchʹ਌͠΋͏ • Grand Central DispatchΛ࢖ͬͨฒߦΞϧΰϦζϜʹೖ໳

Slide 17

Slide 17 text

ฒߦΞϧΰϦζϜͱ͸ • ΞϧΰϦζϜͷ֤෦෼Λฒߦʹॲཧ͢ΔΞϧΰϦζϜ

Slide 18

Slide 18 text

ฒߦΞϧΰϦζϜͱ͸ • ΞϧΰϦζϜͷ֤෦෼Λฒߦʹॲཧ͢ΔΞϧΰϦζϜ

Slide 19

Slide 19 text

ฒߦΞϧΰϦζϜͷ۩ମྫ

Slide 20

Slide 20 text

ΞδΣϯμ • ࠷ۙͷiOSσόΠεͱϚϧνίΞԽʹؔ͢Δ՝୊ • for loopΛฒߦԽ͢Δ • ΫΠοΫιʔτΛฒߦԽ͢Δ • ͍Ζ͍ΖͳΦʔόʔϔου

Slide 21

Slide 21 text

՝୊ • Stringͷ഑ྻͷ .count ͷ߹ܭΛܭࢉ͍ͨ͠

Slide 22

Slide 22 text

՝୊ • Stringͷ഑ྻͷ .count ͷ߹ܭΛܭࢉ͍ͨ͠

Slide 23

Slide 23 text

՝୊ • Stringͷ഑ྻͷ .count ͷ߹ܭΛܭࢉ͍ͨ͠ 36͔ࠃޠͷѫࡰ

Slide 24

Slide 24 text

՝୊ • Stringͷ഑ྻͷ .count ͷ߹ܭΛܭࢉ͍ͨ͠

Slide 25

Slide 25 text

՝୊ • Stringͷ഑ྻͷ .count ͷ߹ܭΛܭࢉ͍ͨ͠

Slide 26

Slide 26 text

՝୊ • Stringͷ഑ྻͷ .count ͷ߹ܭΛܭࢉ͍ͨ͠ map Λฒߦॲཧ͍ͨ͠

Slide 27

Slide 27 text

Grand Central DispatchΛ࢖͑͹ ؆୯ʹॻ͚Δ

Slide 28

Slide 28 text

https://developer.apple.com/documentation/dispatch/dispatchqueue/2016088-concurrentperform

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

ΫϩʔδϟࣜΛฒߦॲཧ

Slide 31

Slide 31 text

doSomething(0), doSomething(1), ..., doSomething(9) ͕ฒߦͰॲཧ͞ΕΔ

Slide 32

Slide 32 text

• DispatchQueue.concurrentPerform ͸forจͷฒߦॲཧ൛ͱݴ͑Δ

Slide 33

Slide 33 text

• DispatchQueue.concurrentPerform ͸forจͷฒߦॲཧ൛ͱݴ͑Δ • forจ͕͋Ε͹mapΛॻ͘ͷ͸؆୯

Slide 34

Slide 34 text

SwiftͱGrandCentralDispatchͰ ฒߦmapΛॻ͍ͯΈΔ

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

nil ͰॳظԽ͓ͯ͘͠

Slide 38

Slide 38 text

͜ͷ෦෼͕ฒྻॲཧ͞ΕΔ

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

unwrap ͯ͠ return

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

ෳ਺εϨου͔Βॻ͖ࠐΈ

Slide 45

Slide 45 text

ෳ਺εϨου͔Βॻ͖ࠐΈ ΫϥογϡͷՄೳੑ͕͋Δ

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

γϦΞϧͳDispatchQueue

Slide 48

Slide 48 text

γϦΞϧͳDispatchQueue ෳ਺εϨου͔Βͷಉ࣌ΞΫηεΛ๷͙

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

ϕϯνϚʔΫ

Slide 51

Slide 51 text

ϕϯνϚʔΫ • 36ϲࠃޠͷѫࡰͷcountΛ߹ܭ

Slide 52

Slide 52 text

ϕϯνϚʔΫ • 36ϲࠃޠͷѫࡰͷcountΛ߹ܭ - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O ) Array.map concurrentMap 0.00024 seconds 0.00064 seconds

Slide 53

Slide 53 text

ϕϯνϚʔΫ • 36ϲࠃޠͷѫࡰͷcountΛ߹ܭ - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O ) Array.map concurrentMap 0.00024 seconds 0.00064 seconds 0 sec 0.001 sec Array.map concurrentMap

Slide 54

Slide 54 text

ϕϯνϚʔΫ • 36ϲࠃޠͷѫࡰͷcountΛ߹ܭ • ฒߦԽͨ͠ͷʹ஗͘ͳ͍ͬͯΔ - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O ) Array.map concurrentMap 0.00024 seconds 0.00064 seconds 0 sec 0.001 sec Array.map concurrentMap

Slide 55

Slide 55 text

ͳͥ஗͘ͳͬͨͷ͔ • ѫࡰఔ౓ͷจࣈ਺Χ΢ϯτͳΒॠ࣌ʹܭࢉͰ͖Δ • Ή͠ΖΦʔόʔϔουͰੑೳ͕མ͍ͪͯΔ

Slide 56

Slide 56 text

ͳͥ஗͘ͳͬͨͷ͔ • ѫࡰఔ౓ͷจࣈ਺Χ΢ϯτͳΒॠ࣌ʹܭࢉͰ͖Δ • Ή͠ΖΦʔόʔϔουͰੑೳ͕མ͍ͪͯΔ γϦΞϧͳQueueʹΑΔɺෳ਺εϨου͔Βͷॻ͖ࠐΈʹର͢Δอޢ

Slide 57

Slide 57 text

݁Ռͷॻ͖ࠐΈ͸ҰͭͷεϨουͷΈ

Slide 58

Slide 58 text

݁Ռͷॻ͖ࠐΈ͸ҰͭͷεϨουͷΈ Φʔόʔϔου

Slide 59

Slide 59 text

ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ࣌ؒ

Slide 60

Slide 60 text

ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ͋ΔॠؒͰͷ഑ྻ΁ͷ୅ೖ͸ɺ
 1ͭͷεϨου͔͠ڐ͞Εͳ͍ ࣌ؒ

Slide 61

Slide 61 text

ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count Overhead Overhead Overhead .count .count .count Overhead Overhead Overhead ͋ΔॠؒͰͷ഑ྻ΁ͷ୅ೖ͸ɺ
 1ͭͷεϨου͔͠ڐ͞Εͳ͍ ࣌ؒ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ

Slide 62

Slide 62 text

ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ଞͷεϨου͕ॻ͖ࠐΜͰ͍Δ࣌ɺ
 ͦΕ͕ऴΘΔ·Ͱ଴ͭඞཁ͕͋Δ ࣌ؒ

Slide 63

Slide 63 text

ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ଞͷεϨου͕ॻ͖ࠐΜͰ͍Δ࣌ɺ
 ͦΕ͕ऴΘΔ·Ͱ଴ͭඞཁ͕͋Δ ଴ͪ࣌ؒ: Φʔόʔϔου ࣌ؒ

Slide 64

Slide 64 text

ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ଞͷεϨου͕ॻ͖ࠐΜͰ͍Δ࣌ɺ
 ͦΕ͕ऴΘΔ·Ͱ଴ͭඞཁ͕͋Δ ଴ͪ࣌ؒ: Φʔόʔϔου ࣌ؒ

Slide 65

Slide 65 text

ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ฒߦԽͷԸܙΑΓ Φʔόʔϔουͷํ͕ϖφϧςΟ͕େ͖͍ ࣌ؒ

Slide 66

Slide 66 text

ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead < Ըܙ ϖφϧςΟ ࣌ؒ

Slide 67

Slide 67 text

ίΞ0 ίΞ1 ίΞ2 ίΞ3 .count .count .count .count ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ Overhead Overhead Overhead .count .count .count .count Overhead ഑ྻ΁ͷ୅ೖ Overhead Overhead ҰͭҰͭͷλεΫ ͕খ͗͢͞Δ ࣌ؒ

Slide 68

Slide 68 text

ίΞ0 ίΞ1 ίΞ2 ίΞ3 λεΫ λεΫ λεΫ λεΫ Overhead Overhead Overhead λεΫ λεΫ λεΫ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ λεΫ ࣌ؒ

Slide 69

Slide 69 text

ίΞ0 ίΞ1 ίΞ2 ίΞ3 λεΫ λεΫ λεΫ λεΫ Overhead Overhead Overhead λεΫ λεΫ λεΫ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ ഑ྻ΁ͷ୅ೖ λεΫ > Ըܙ ϖφϧςΟ Φʔόʔϔουͷ઎ΊΔׂ߹ΛݮΒ͢ ࣌ؒ

Slide 70

Slide 70 text

ॏ͍ॲཧͰϕϯνϚʔΫ

Slide 71

Slide 71 text

ॏ͍ॲཧͰϕϯνϚʔΫ • Ն໨ᕸੴͷʮ͜͜Ζʯͷ๯಄ͷ 57631 จࣈ͔Β׽ࣈͷݸ਺ΛΧ΢ϯτ • ഑ྻͷཁૉʹ͸1ஈམΛ·Δ͝ͱೖΕͯܭଌ

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

ॏ͍ॲཧͰϕϯνϚʔΫ • Ն໨ᕸੴͷʮ͜͜Ζʯͷ๯಄ͷ 57631 จࣈ͔Β׽ࣈͷݸ਺ΛΧ΢ϯτ • ഑ྻͷཁૉʹ͸1ஈམΛ·Δ͝ͱೖΕͯܭଌ

Slide 75

Slide 75 text

ॏ͍ॲཧͰϕϯνϚʔΫ • Ն໨ᕸੴͷʮ͜͜Ζʯͷ๯಄ͷ 57631 จࣈ͔Β׽ࣈͷݸ਺ΛΧ΢ϯτ • ഑ྻͷཁૉʹ͸1ஈམΛ·Δ͝ͱೖΕͯܭଌ - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ
 ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O ) Array.map concurrentMap 0.0082 seconds 0.0047 seconds

Slide 76

Slide 76 text

ॏ͍ॲཧͰϕϯνϚʔΫ • Ն໨ᕸੴͷʮ͜͜Ζʯͷ๯಄ͷ 57631 จࣈ͔Β׽ࣈͷݸ਺ΛΧ΢ϯτ • ഑ྻͷཁૉʹ͸1ஈམΛ·Δ͝ͱೖΕͯܭଌ - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ
 ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O ) Array.map concurrentMap 0.0082 seconds 0.0047 seconds 0 sec 0.01 sec Array.map concurrentMap

Slide 77

Slide 77 text

஫ҙ఺ ฒߦԽ͸ສೳͰ͸ͳ͍ • े෼ʹॏ͍ॲཧΛฒߦԽ͠ͳ͍ͱҙຯ͕ͳ͍ɻ • ฒߦԽͷԸܙ͕ɺΦʔόʔϔουʹΑΔϖφϧςΟΛ্ճΔඞཁ͕͋Δɻ

Slide 78

Slide 78 text

஫ҙ఺ ฒߦԽ͸ສೳͰ͸ͳ͍ • े෼ʹॏ͍ॲཧΛฒߦԽ͠ͳ͍ͱҙຯ͕ͳ͍ɻ • ฒߦԽͷԸܙ͕ɺΦʔόʔϔουʹΑΔϖφϧςΟΛ্ճΔඞཁ͕͋Δɻ • ฒߦԽͷԸܙΛड͚ΒΕΔΑ͏ʹλεΫΛׂΓৼΔඞཁ͕͋Δɻ

Slide 79

Slide 79 text

ͳʹ͕ѱ͔ͬͨͷ͔ ࠷ॳͷྫʹ໭Δ

Slide 80

Slide 80 text

ͳʹ͕ѱ͔ͬͨͷ͔ ࠷ॳͷྫʹ໭Δ • ҰͭҰͭͷλεΫ(഑ྻͷཁૉ͕ࡉ੾Εʹͳ͍ͬͯΔ)

Slide 81

Slide 81 text

ͳʹ͕ѱ͔ͬͨͷ͔ ࠷ॳͷྫʹ໭Δ • ҰͭҰͭͷλεΫ(഑ྻͷཁૉ͕ࡉ੾Εʹͳ͍ͬͯΔ) • ຊ౰ʹ΍Γ͍ͨλεΫΑΓΦʔόʔϔουʹ࣌ؒΛඅ΍ͯ͠͠·ͬͨ

Slide 82

Slide 82 text

͜͜·Ͱͷ·ͱΊ • DispatchQueue.concurrentPerform͸forจͷฒߦॲཧ൛ • ฒߦԽʹͱ΋ͳ͍ɺಛผͳέΞ͕ඞཁʹͳΔ͜ͱ͕͋Δ

Slide 83

Slide 83 text

͜͜·Ͱͷ·ͱΊ • DispatchQueue.concurrentPerform͸forจͷฒߦॲཧ൛ • ฒߦԽʹͱ΋ͳ͍ɺಛผͳέΞ͕ඞཁʹͳΔ͜ͱ͕͋Δ • CPUͷίΞ͕ຊ౰ʹ΍Γ͍ͨ͜ͱʹ࣌ؒΛඅ΍ͤΔΑ͏ʹ͓હཱͯͯ͋͛͠ Δͷ͕๻ͨͪͷ࢓ࣄ

Slide 84

Slide 84 text

ΞδΣϯμ • ࠷ۙͷiOSσόΠεͱϚϧνίΞԽʹؔ͢Δ՝୊ • for loopΛฒߦԽ͢Δ • ΫΠοΫιʔτΛฒߦԽ͢Δ • ͍Ζ͍ΖͳΦʔόʔϔου

Slide 85

Slide 85 text

ΫΠοΫιʔτ

Slide 86

Slide 86 text

array 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 59 75 92 99 77 15 13 2 68 31 83 35 67 51 70

Slide 87

Slide 87 text

array 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 59 75 92 99 77 15 13 2 68 31 83 35 67 51 70 pivot

Slide 88

Slide 88 text

array 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 59 75 92 99 77 15 13 2 68 31 83 35 67 51 70 array 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 13 92 2 31 35 51 59 75 99 77 68 83 67 70 Partition

Slide 89

Slide 89 text

array 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 59 75 92 99 77 15 13 2 68 31 83 35 67 51 70 array 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 13 92 2 31 35 51 59 75 99 77 68 83 67 70 <= 59 Partition

Slide 90

Slide 90 text

array 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 59 75 92 99 77 15 13 2 68 31 83 35 67 51 70 array 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 13 92 2 31 35 51 59 75 99 77 68 83 67 70 <= 59 59 < Partition

Slide 91

Slide 91 text

array 0 1 2 3 4 5 15 13 2 31 35 51 14 7 8 9 10 11 12 13 92 75 99 77 68 83 67 70

Slide 92

Slide 92 text

array 0 1 2 3 4 5 15 13 2 31 35 51 14 7 8 9 10 11 12 13 92 75 99 77 68 83 67 70 pivot pivot

Slide 93

Slide 93 text

array 0 1 2 3 4 5 15 13 2 31 35 51 Partition 14 7 8 9 10 11 12 13 92 75 99 77 68 83 67 70 Partition

Slide 94

Slide 94 text

array 0 1 2 3 4 5 15 13 2 31 35 51 Partition array 0 1 2 3 4 5 2 13 15 31 35 51 14 7 8 9 10 11 12 13 92 75 99 77 68 83 67 70 Partition 14 7 8 9 10 11 12 13 99 67 68 70 75 77 83 92

Slide 95

Slide 95 text

array 0 1 2 3 4 5 15 13 2 31 35 51 Partition array 0 1 2 3 4 5 2 13 15 31 35 51 14 7 8 9 10 11 12 13 92 75 99 77 68 83 67 70 Partition 14 7 8 9 10 11 12 13 99 67 68 70 75 77 83 92

Slide 96

Slide 96 text

59 99 75 92 15 13 2 31 35 68 83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 2 13 31 35 51 59 99 35 75 83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 2 15 13 31 35 51 a 0 1 2 3 4 5 68 75 67 70 92 83 77 99 14 7 8 9 10 11 12 13 a 2 13 a 0 1 31 35 51 3 4 5 35 51 4 5 51 5 13 1 68 67 70 7 8 9 68 70 8 9 70 9 83 77 92 99 14 11 12 13 99 14 77 83 11 12 83 12

Slide 97

Slide 97 text

59 99 75 92 15 13 2 31 35 68 83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 2 13 31 35 51 99 35 75 83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 2 13 31 35 51 a 0 1 2 3 4 5 68 67 70 92 83 77 99 14 7 8 9 10 11 12 13 a 13 a 0 1 35 51 3 4 5 51 4 5 5 1 68 70 7 8 9 70 8 9 9 83 77 99 14 11 12 13 14 83 11 12 12 59 15 75 2 31 35 51 13 67 68 70 92 99 77 83

Slide 98

Slide 98 text

59 15 75 2 31 35 51 13 67 68 70 92 99 77 83

Slide 99

Slide 99 text

59 15 75 2 31 35 51 13 67 68 70 92 99 77 83

Slide 100

Slide 100 text

ΫΠοΫιʔτͷΞϧΰϦζϜ

Slide 101

Slide 101 text

ΫΠοΫιʔτͷΞϧΰϦζϜ array 0 1 2 3 4 5 6 7 5 2 1 4 6 8 7 3

Slide 102

Slide 102 text

ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ array 0 1 2 3 4 5 6 7 5 2 1 4 6 8 7 3

Slide 103

Slide 103 text

ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6

Slide 104

Slide 104 text

ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 ύʔςΟγϣϯͱ΋ݺͿ

Slide 105

Slide 105 text

ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6

Slide 106

Slide 106 text

ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6

Slide 107

Slide 107 text

ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 2 1 4 3

Slide 108

Slide 108 text

ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 2 1 4 3

Slide 109

Slide 109 text

ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 2 1 4 3

Slide 110

Slide 110 text

ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 1 2 4 3

Slide 111

Slide 111 text

ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ 4. ϐϘοτΑΓେ͖͍෦෼ʹ1, 2Λ܁Γฦ͢ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 1 2 4 3 array 5 6 7 8 7 6

Slide 112

Slide 112 text

ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ 4. ϐϘοτΑΓେ͖͍෦෼ʹ1, 2Λ܁Γฦ͢ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 1 2 4 3 array 5 6 7 8 7 6

Slide 113

Slide 113 text

ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ 4. ϐϘοτΑΓେ͖͍෦෼ʹ1, 2Λ܁Γฦ͢ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 1 2 4 3 array 5 6 7 6 7 8

Slide 114

Slide 114 text

ΫΠοΫιʔτͷΞϧΰϦζϜ 1. ϐϘοτΛબͼ 2. ഑ྻΛϐϘοτҎԼͷ෦෼ͱϐϘοτΑΓେ͖͍෦෼ͱʹ෼͚Δ 3. ϐϘοτҎԼͷ෦෼ʹ1, 2Λ܁Γฦ͢ 4. ϐϘοτΑΓେ͖͍෦෼ʹ1, 2Λ܁Γฦ͢ 5. Ҏ্Λ࠶ىతʹ܁Γฦ͢ array 0 1 2 3 4 5 6 7 2 1 4 3 5 8 7 6 array 0 1 2 3 1 2 4 3 array 5 6 7 6 7 8

Slide 115

Slide 115 text

ΫΠοΫιʔτΛฒߦԽ͢Δ

Slide 116

Slide 116 text

ΫΠοΫιʔτΛฒߦԽ͢Δ

Slide 117

Slide 117 text

ݴ͍׵͑Δͱɺ

Slide 118

Slide 118 text

ΫΠοΫιʔτͷΞϧΰϦζϜͷத͔Βɺ ฒߦԽ͢Δ͜ͱ͕Ͱ͖ΔλεΫΛݟ͚ͭΔ

Slide 119

Slide 119 text

ฒߦԽ͢Δ͜ͱ͕Ͱ͖ΔλεΫͱ͸

Slide 120

Slide 120 text

ฒߦԽ͢Δ͜ͱ͕Ͱ͖ΔλεΫͱ͸ • ޓ͍ʹಠཱͨ͠ॲཧ͸ฒߦԽ͢Δ͜ͱ͕Ͱ͖Δ • ಠཱͨ͠ॲཧ͕શͯऴΘͬͨޙɺͦΕͧΕΛ·ͱΊͯҰͭͷ݁ՌΛಘΔ

Slide 121

Slide 121 text

ฒߦԽ͢Δ͜ͱ͕Ͱ͖ΔλεΫͱ͸ • ޓ͍ʹಠཱͨ͠ॲཧ͸ฒߦԽ͢Δ͜ͱ͕Ͱ͖Δ • ಠཱͨ͠ॲཧ͕શͯऴΘͬͨޙɺͦΕͧΕΛ·ͱΊͯҰͭͷ݁ՌΛಘΔ

Slide 122

Slide 122 text

ฒߦԽ͢Δ͜ͱ͕Ͱ͖ΔλεΫͱ͸ • ޓ͍ʹಠཱͨ͠ॲཧ͸ฒߦԽ͢Δ͜ͱ͕Ͱ͖Δ • ಠཱͨ͠ॲཧ͕શͯऴΘͬͨޙɺͦΕͧΕΛ·ͱΊͯҰͭͷ݁ՌΛಘΔ ΫΠοΫιʔτͷத͔Βಠཱͨ͠ ॲཧΛݟ͚ͭΑ͏

Slide 123

Slide 123 text

59 99 75 92 15 13 2 31 35 68 83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 2 13 31 35 51 59 99 35 75 83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 2 15 13 31 35 51 a 0 1 2 3 4 5 68 75 67 70 92 83 77 99 14 7 8 9 10 11 12 13 a 2 13 a 0 1 31 35 51 3 4 5 35 51 4 5 51 5 13 1 68 67 70 7 8 9 68 70 8 9 70 9 83 77 92 99 14 11 12 13 99 14 77 83 11 12 83 12

Slide 124

Slide 124 text

59 99 75 92 15 13 2 31 35 68 83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 15 2 13 31 35 51 59 99 35 75 83 67 51 70 77 a 0 14 1 2 3 4 5 6 7 8 9 10 11 12 13 2 15 13 31 35 51 a 0 1 2 3 4 5 68 75 67 70 92 83 77 99 14 7 8 9 10 11 12 13 a 2 13 a 0 1 31 35 51 3 4 5 35 51 4 5 51 5 13 1 68 67 70 7 8 9 68 70 8 9 70 9 83 77 92 99 14 11 12 13 99 14 77 83 11 12 83 12

Slide 125

Slide 125 text

15 2 13 31 35 51 99 35 75 83 67 51 70 77 a 0 14 1 2 3 4 5 7 8 9 10 11 12 13 2 15 13 31 35 51 a 0 1 2 3 4 5 68 75 67 70 92 83 77 99 14 7 8 9 10 11 12 13 a 2 13 a 0 1 31 35 51 3 4 5 35 51 4 5 51 5 13 1 68 67 70 7 8 9 68 70 8 9 70 9 83 77 92 99 14 11 12 13 99 14 77 83 11 12 83 12 2ͭͷಠཱͨ͠ΫΠοΫιʔτ

Slide 126

Slide 126 text

2 15 13 31 35 51 a 0 1 2 3 4 5 68 75 67 70 92 83 77 99 14 7 8 9 10 11 12 13 a 2 13 a 0 1 31 35 51 3 4 5 35 51 4 5 51 5 13 1 68 67 70 7 8 9 68 70 8 9 70 9 83 77 92 99 14 11 12 13 99 14 77 83 11 12 83 12 4ͭͷಠཱͨ͠ΫΠοΫιʔτ

Slide 127

Slide 127 text

ಠཱͨ͠λεΫ͸ฒߦʹॲཧͰ͖Δʂ

Slide 128

Slide 128 text

SwiftͱGrand Central DispatchͰ ࣮૷ͯ͠ΈΔ

Slide 129

Slide 129 text

௚ྻॲཧ൛ͷΫΠοΫιʔτ ͭ·ΓɺฒߦԽ͢ΔલͷΫΠοΫιʔτ

Slide 130

Slide 130 text

௚ྻॲཧ൛ͷΫΠοΫιʔτ

Slide 131

Slide 131 text

Array͕ࢀর͍ͯ͠Δ όοϑΝʔʹ௚઀ΞΫηε ௚ྻॲཧ൛ͷΫΠοΫιʔτ

Slide 132

Slide 132 text

௚ྻॲཧ൛ͷΫΠοΫιʔτ

Slide 133

Slide 133 text

௚ྻॲཧ൛ͷΫΠοΫιʔτ

Slide 134

Slide 134 text

௚ྻॲཧ൛ͷΫΠοΫιʔτ

Slide 135

Slide 135 text

ύʔςΟγϣϯॲཧ ௚ྻॲཧ൛ͷΫΠοΫιʔτ

Slide 136

Slide 136 text

഑ྻͷϐϘοτҎԼͷ෦෼ ௚ྻॲཧ൛ͷΫΠοΫιʔτ

Slide 137

Slide 137 text

഑ྻͷϐϘοτΑΓେ͖͍෦෼ ௚ྻॲཧ൛ͷΫΠοΫιʔτ

Slide 138

Slide 138 text

௚ྻॲཧ൛ͷΫΠοΫιʔτ

Slide 139

Slide 139 text

͜͜ΛฒߦԽ͢Ε͹ྑ͍͸ͣʂ ௚ྻॲཧ൛ͷΫΠοΫιʔτ

Slide 140

Slide 140 text

DispatchQueueΛ࢖ͬͯฒߦԽ

Slide 141

Slide 141 text

DispatchQueueΛ࢖ͬͯฒߦԽ

Slide 142

Slide 142 text

DispatchQueueΛ࢖ͬͯฒߦԽ

Slide 143

Slide 143 text

DispatchQueueΛ࢖ͬͯฒߦԽ

Slide 144

Slide 144 text

DispatchQueueΛ࢖ͬͯฒߦԽ

Slide 145

Slide 145 text

DispatchQueueΛ࢖ͬͯฒߦԽ

Slide 146

Slide 146 text

DispatchQueueΛ࢖ͬͯฒߦԽ `async` ͭ·ΓɺඇಉظॲཧͳͷͰɺ ιʔτॲཧ͕ऴΘΔલʹؔ਺Λൈ͚ͯ͠·͏ɻ

Slide 147

Slide 147 text

DispatchQueueΛ࢖ͬͯฒߦԽ

Slide 148

Slide 148 text

DispatchQueueΛ࢖ͬͯฒߦԽ

Slide 149

Slide 149 text

DispatchQueueΛ࢖ͬͯฒߦԽ ཪͷεϨουͰιʔτ͕ߦΘΕ͍ͯΔ ࠷தʹΞΫηε

Slide 150

Slide 150 text

DispatchQueueΛ࢖ͬͯฒߦԽ ☠

Slide 151

Slide 151 text

ඇಉظॲཧ͕׬ྃ͢Δ·Ͱ଴ͭඞཁ͕͋Δ

Slide 152

Slide 152 text

ඇಉظॲཧ͕׬ྃ͢Δ·Ͱ଴ͭඞཁ͕͋Δ • DispatchGroup͕࢖͑Δɻ • DispatchGroupΛDispatchQueue.async ͷҾ਺ʹ౉͢ͱɺඇಉظॲཧ͕׬ྃ͢ Δ·ͰεϨουΛϒϩοΫͯ͘͠ΕΔɻ

Slide 153

Slide 153 text

DispatchGroupͰඇಉظॲཧͷ׬ྃΛ଴ͭ

Slide 154

Slide 154 text

DispatchGroupͰඇಉظॲཧͷ׬ྃΛ଴ͭ

Slide 155

Slide 155 text

DispatchGroupͰඇಉظॲཧͷ׬ྃΛ଴ͭ

Slide 156

Slide 156 text

DispatchGroupͰඇಉظॲཧͷ׬ྃΛ଴ͭ

Slide 157

Slide 157 text

DispatchGroupͰඇಉظॲཧͷ׬ྃΛ଴ͭ શͯͷඇಉظॲཧ͕ऴΘΔ·ͰϒϩοΫ

Slide 158

Slide 158 text

DispatchGroupͰඇಉظॲཧͷ׬ྃΛ଴ͭ

Slide 159

Slide 159 text

ϕϯνϚʔΫ • 1ԯݸͷϥϯμϜͳ੔਺ͷ഑ྻΛιʔτ

Slide 160

Slide 160 text

ϕϯνϚʔΫ • 1ԯݸͷϥϯμϜͳ੔਺ͷ഑ྻΛιʔτ ඪ४ϥΠϒϥϦ
 Array.sort() ௚ྻ൛ ΫΠοΫιʔτ ฒߦ൛ ΫΠοΫιʔτ 10.3 seconds 9.0 seconds 8.1 seconds - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O )

Slide 161

Slide 161 text

࣌ؒ 0 sec 2 sec 4 sec 6 sec 8 sec 10 sec 12 sec Array.sort() ௚ྻΫΠοΫιʔτ ฒߦΫΠοΫιʔτ

Slide 162

Slide 162 text

࣌ؒ 0 sec 2 sec 4 sec 6 sec 8 sec 10 sec 12 sec Array.sort() ௚ྻΫΠοΫιʔτ ฒߦΫΠοΫιʔτ ໿10ඵ ໿9ඵ ໿8ඵ

Slide 163

Slide 163 text

No content

Slide 164

Slide 164 text

͋Μ·Γ଎͘ͳ͍ɻ

Slide 165

Slide 165 text

ͳͥ଎͘ͳ͍ͷ͔

Slide 166

Slide 166 text

ͳͥ଎͘ͳ͍ͷ͔

Slide 167

Slide 167 text

ͳͥ଎͘ͳ͍ͷ͔ ๲େͳ਺ͷΫϩʔδϟΛ enqueue ͍ͯ͠Δ

Slide 168

Slide 168 text

Dispatch Queue

Slide 169

Slide 169 text

Dispatch Queue { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() }

Slide 170

Slide 170 text

Dispatch Queue { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } CPU ίΞ CPU ίΞ CPU ίΞ

Slide 171

Slide 171 text

Dispatch Queue { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } CPU ίΞ CPU ίΞ CPU ίΞ

Slide 172

Slide 172 text

Dispatch Queue { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } CPU ίΞ CPU ίΞ CPU ίΞ ίΞͷୣ͍߹͍

Slide 173

Slide 173 text

Dispatch Queue { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } { quickSort() quickSort() } CPU ίΞ CPU ίΞ CPU ίΞ ίΞͷୣ͍߹͍ Φʔόʔϔου

Slide 174

Slide 174 text

SemaphoreΛ࢖ͬͯϦιʔεΛ؅ཧ

Slide 175

Slide 175 text

SemaphoreΛ࢖ͬͯϦιʔεΛ؅ཧ • Semaphore͸தʹIntܕͷ਺஋Λ͍࣋ͬͯͯɺϦιʔεͷར༻Մ൱Λ؅ཧ • ࠓճ͸CPUͷίΞΛϦιʔεͱߟ͑Δ • `signal()` ͰϦιʔε(ίΞ)Λղ์͢Δ • `wait()`ͰϦιʔε(ίΞ)Λ࢖༻͢Δ

Slide 176

Slide 176 text

8

Slide 177

Slide 177 text

8

Slide 178

Slide 178 text

࢖༻ՄೳͳϦιʔεͷॳظ஋ 8

Slide 179

Slide 179 text

Ϧιʔε(ίΞ)͕࢖༻Մೳ͔νΣοΫ 8

Slide 180

Slide 180 text

ར༻ՄೳͳΒenqueue 8

Slide 181

Slide 181 text

ΫϩʔδϟΛൈ͚Δͱ͖ʹ SignalΛૹͬͯར༻׬ྃΛ௨஌ 8

Slide 182

Slide 182 text

ར༻ෆՄͳΒಉ͡εϨουͰॲཧΛܧଓ 8

Slide 183

Slide 183 text

݁Ռ • 1ԯݸͷϥϯμϜͳ੔਺ͷ഑ྻΛιʔτ ඪ४ϥΠϒϥϦ
 Array.sort() ௚ྻ൛ ΫΠοΫιʔτ ฒߦ൛ ΫΠοΫιʔτ ฒߦ൛ ΫΠοΫιʔτ
 with Semaphore 10.3 seconds 9.0 seconds 8.1 seconds 4.5 seconds - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O )

Slide 184

Slide 184 text

࣌ؒ 0 sec 2 sec 4 sec 6 sec 8 sec 10 sec 12 sec Array.sort() ௚ྻΫΠοΫιʔτ ฒߦΫΠοΫιʔτ ฒߦΫΠοΫιʔτվ ໿10ඵ ໿9ඵ ໿8ඵ ໿4.5ඵ

Slide 185

Slide 185 text

2ഒҎ্ͷߴ଎Խ Swiftͷඪ४ϥΠϒϥϦͱൺֱ

Slide 186

Slide 186 text

2ഒҎ্ͷߴ଎Խʂ

Slide 187

Slide 187 text

2ഒҎ্ͷߴ଎Խʂ • SemaphoreͰϦιʔε(ίΞ)ͷར༻Λ੍ޚ͢Δ͜ͱʹΑͬͯΦʔόʔϔουΛ ݮΒͨ͠ɻ

Slide 188

Slide 188 text

2ഒҎ্ͷߴ଎Խʂ • SemaphoreͰϦιʔε(ίΞ)ͷར༻Λ੍ޚ͢Δ͜ͱʹΑͬͯΦʔόʔϔουΛ ݮΒͨ͠ɻ • Semaphoreͷॳظ஋ -> ར༻Ͱ͖ΔϦιʔεͷ਺

Slide 189

Slide 189 text

8

Slide 190

Slide 190 text

8 ηϚϑΥͷॳظ஋: ಉ࣌ʹར༻͞ΕΔίΞͷ਺

Slide 191

Slide 191 text

• SemaphoreͷΠχγϟϥΠβͷҾ਺͕ɺಉ࣌ʹར༻͞ΕΔίΞͷ਺

Slide 192

Slide 192 text

• SemaphoreͷΠχγϟϥΠβͷҾ਺͕ɺಉ࣌ʹར༻͞ΕΔίΞͷ਺ • ಉ࣌ʹར༻͞ΕΔίΞͷ਺Λม͑ͨΒɺॲཧͷ଎͕͞มΘΔ͸ͣ

Slide 193

Slide 193 text

Semaphoreͷॳظ஋Ͱ଎͞Λൺֱ 6ίΞ12εϨουͷMacBook ProͰܭଌ

Slide 194

Slide 194 text

Semaphoreͷॳظ஋Ͱ଎͞Λൺֱ 6ίΞ12εϨουͷMacBook ProͰܭଌ 0 2.5 5 7.5 10 12.5 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - 1ԯݸͷϥϯμϜͳ੔਺Λιʔτɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O )

Slide 195

Slide 195 text

Semaphoreͷॳظ஋Ͱ଎͞Λൺֱ 6ίΞ12εϨουͷMacBook ProͰܭଌ 0 2.5 5 7.5 10 12.5 15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - 1ԯݸͷϥϯμϜͳ੔਺Λιʔτɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O ) ཧ૝

Slide 196

Slide 196 text

100%ཧ૝௨Γʹ͸͍͔ͳ͍

Slide 197

Slide 197 text

100%ཧ૝௨Γʹ͸͍͔ͳ͍ • ར༻͢ΔίΞ͕2ഒʹͳΕ͹ɺ଎͞΋2ഒʹͳΔͷ͕100%ͷཧ૝ • ͦͷͨΊʹ͸ΦʔόʔϔουΛθϩʹ͠ͳ͍ͱ͍͚ͳ͍

Slide 198

Slide 198 text

100%ཧ૝௨Γʹ͸͍͔ͳ͍ • ར༻͢ΔίΞ͕2ഒʹͳΕ͹ɺ଎͞΋2ഒʹͳΔͷ͕100%ͷཧ૝ • ͦͷͨΊʹ͸ΦʔόʔϔουΛηϩʹ͠ͳ͍ͱ͍͚ͳ͍ • ΦʔόʔϔουΛ׬શʹθϩʹ͢Δͷ͸ݱ࣮తʹ͸ແཧ

Slide 199

Slide 199 text

100%ཧ૝௨Γʹ͸͍͔ͳ͍ • ར༻͢ΔίΞ͕2ഒʹͳΕ͹ɺ଎͞΋2ഒʹͳΔͷ͕100%ͷཧ૝ • ͦͷͨΊʹ͸ΦʔόʔϔουΛηϩʹ͠ͳ͍ͱ͍͚ͳ͍ • ΦʔόʔϔουΛ׬શʹθϩʹ͢Δͷ͸ݱ࣮తʹ͸ແཧ • ཧ૝௨Γʹ͸͍͔ͳ͍

Slide 200

Slide 200 text

ΞδΣϯμ • ࠷ۙͷiOSσόΠεͱϚϧνίΞԽʹؔ͢Δ՝୊ • for loopΛฒߦԽ͢Δ • ΫΠοΫιʔτΛฒߦԽ͢Δ • ͍Ζ͍ΖͳΦʔόʔϔου

Slide 201

Slide 201 text

ͦ΋ͦ΋Φʔόʔϔουͱ͸

Slide 202

Slide 202 text

ͦ΋ͦ΋Φʔόʔϔουͱ͸ • ΋ͱ΋ͱͷ௚ྻΞϧΰϦζϜʹ͸ͳ͔ͬͨ௥Ճͷॲཧɺ͓ΑͼͦΕʹ͔͔Δ ࣌ؒɻ

Slide 203

Slide 203 text

ͦ΋ͦ΋Φʔόʔϔουͱ͸ • ΋ͱ΋ͱͷ௚ྻΞϧΰϦζϜʹ͸ͳ͔ͬͨ௥Ճͷॲཧɺ͓ΑͼͦΕʹ͔͔Δ ࣌ؒɻ • ෳ਺εϨου͔Βͷॻ͖ࠐΈͷอޢɺGCDϝιουͷݺͼग़͠ͳͲɺฒߦԽ ͷͨΊʹඞཁͳॲཧʹ͔͔Δ࣌ؒશͯ

Slide 204

Slide 204 text

ͦ΋ͦ΋Φʔόʔϔουͱ͸ • ΋ͱ΋ͱͷ௚ྻΞϧΰϦζϜʹ͸ͳ͔ͬͨ௥Ճͷॲཧɺ͓ΑͼͦΕʹ͔͔Δ ࣌ؒɻ • ෳ਺εϨου͔Βͷॻ͖ࠐΈͷอޢɺGCDϝιουͷݺͼग़͠ͳͲɺฒߦԽ ͷͨΊʹඞཁͳॲཧʹ͔͔Δ࣌ؒશͯ • ͦͷଞɺؾ෇͖ʹ͍͘΋ͷͱͯ͠False Sharingͱݺ͹ΕΔ΋ͷ΋͋Δ

Slide 205

Slide 205 text

False Sharing

Slide 206

Slide 206 text

No content

Slide 207

Slide 207 text

No content

Slide 208

Slide 208 text

No content

Slide 209

Slide 209 text

ผͷεϨουͰ sum() ͱ inc() ΛݺͿ

Slide 210

Slide 210 text

0.002 ඵ

Slide 211

Slide 211 text

0.002 ඵ

Slide 212

Slide 212 text

0.002 ඵ

Slide 213

Slide 213 text

0.002 ඵ 0.2 ඵ

Slide 214

Slide 214 text

0.002 ඵ 0.2 ඵ ໿100ഒ

Slide 215

Slide 215 text

0.002 ඵ 0.2 ඵ ໿100ഒ - 5ճଌఆͨ͠ฏۉ஋Ͱ͢ɻ - εϥΠυʹ৐ͤͨίʔυ͸આ໌༻Ͱ͢ɻ࣮ࡍʹଌఆʹར༻ ͨ͠ίʔυ͸Githubʹ͋Γ·͢ɻ - MacBook Pro 15-inch, 2018, Core i9 2.9 GHz. - macOS Catalina 10.15.6, Swift 5.2.4, ࠷దԽ͋Γ ( -O )

Slide 216

Slide 216 text

Core 0 Core 1

Slide 217

Slide 217 text

Core 0 Core 1 sum() inc()

Slide 218

Slide 218 text

x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc()

Slide 219

Slide 219 text

x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache Cache x y

Slide 220

Slide 220 text

x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache Cache x y ֤ίΞʹΩϟογϡ͕ උΘ͍ͬͯΔ

Slide 221

Slide 221 text

x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache Cache x y

Slide 222

Slide 222 text

x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache Cache x y ͦΕͧΕͷίΞͷ ΩϟογϡʹΞΫηε

Slide 223

Slide 223 text

x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc()

Slide 224

Slide 224 text

x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() ϝϞϦ্ͰྡΓ߹ͬͨ Ґஔʹ͋Δ

Slide 225

Slide 225 text

x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache Cache x y x y

Slide 226

Slide 226 text

x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache Cache x y x y

Slide 227

Slide 227 text

x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache Cache x y x y Yʹରͯ͠ॻ͖ࠐΈ

Slide 228

Slide 228 text

x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache Cache x y x y'

Slide 229

Slide 229 text

x y' ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache Cache x y' x y' ͪ͜ΒͷΩϟογϡ΋ ߋ৽ͯ͠͠·͏

Slide 230

Slide 230 text

x y ϝΠϯϝϞϦ Core 0 Core 1 sum() inc() Cache Cache x y' x y' Φʔόʔϔου ͪ͜ΒͷΩϟογϡ΋ ߋ৽ͯ͠͠·͏

Slide 231

Slide 231 text

False Sharing ೔ຊޠͰ͸ِڞ༗(͖͗ΐ͏Ώ͏)͋Δ͍͸ෆਖ਼ڞ༗

Slide 232

Slide 232 text

False Sharing ೔ຊޠͰ͸ِڞ༗(͖͗ΐ͏Ώ͏)͋Δ͍͸ෆਖ਼ڞ༗ • xͱy͸ϓϩάϥϜͷҙຯ্͸ແؔ܎ͷ͸ͣ • ͔͠͠ɺxͱy͸ϋʔυ΢ΣΞతʹ͸ಉ͡ΩϟογϡϥΠϯ্ʹଘࡏ͢Δ • ϋʔυ΢ΣΞ͸ΩϟογϡϥΠϯΛ୯ҐʹΩϟογϡͷߋ৽Λߦ͏ͨΊɺ
 yͷߋ৽͝ͱʹଞͷίΞͷΩϟογϡ΋ߋ৽͠ͳ͚Ε͹ͳΒͳ͍

Slide 233

Slide 233 text

False Sharing ೔ຊޠͰ͸ِڞ༗(͖͗ΐ͏Ώ͏)͋Δ͍͸ෆਖ਼ڞ༗ • xͱy͸ϓϩάϥϜͷҙຯ্͸ແؔ܎ͷ͸ͣ • ͔͠͠ɺxͱy͸ϋʔυ΢ΣΞతʹ͸ಉ͡ΩϟογϡϥΠϯ্ʹଘࡏ͢Δ • ϋʔυ΢ΣΞ͸ΩϟογϡϥΠϯΛ୯ҐʹΩϟογϡͷߋ৽Λߦ͏ͨΊɺ
 yͷߋ৽͝ͱʹଞͷίΞͷΩϟογϡ΋ߋ৽͠ͳ͚Ε͹ͳΒͳ͍ Φʔόʔϔου

Slide 234

Slide 234 text

࠷ޙʹ

Slide 235

Slide 235 text

ฒߦΞϧΰϦζϜͱͷ෇͖߹͍ํ • ͍͖ͳΓฒߦΞϧΰϦζϜ͔Β࢝ΊΔͷ͸͓͢͢ΊͰ͖ͳ͍ɻ • ·ͣ͸௚ྻͷΞϧΰϦζϜ͔ΒॳΊͯɺύϑΥʔϚϯεʹ໰୊͕͋Ε͹ฒߦΞ ϧΰϦζϜΛݕ౼͢Δͷ͕ྑ͍ɻ • ฒߦΞϧΰϦζϜ͸ɺਖ਼௚ʹݴͬͯ؆୯Ͱ͸ͳ͍ͷͰɺ͍͖ͳΓ࣮૷Λ࢝ΊΔ લʹઐ໳ͷॻ੶ΛಡΜͰΈΔͷ͕͓͢͢ΊͰ͢ɻ

Slide 236

Slide 236 text

Acknowledgements ࢀߟࢿྉ

Slide 237

Slide 237 text

Acknowledgements ฒߦΞϧΰϦζϜʹ͍ͭͯͷࢀߟॻ੶ • The Art of Concurrency • ࿨༁: ฒߦίϯϐϡʔςΟϯάٕ๏

Slide 238

Slide 238 text

Acknowledgements Concurrent Mapʹ͍ͭͯ • objc.io, Concurrent Map • https://talk.objc.io/episodes/S01E90-concurrent-map

Slide 239

Slide 239 text

Acknowledgements False Sharingʹ͍ͭͯ • https://www.isus.jp/products/vtune/pu38-02-detecting-and-mitigating-false- sharing/

Slide 240

Slide 240 text

·ͱΊ • ΞϧΰϦζϜͷத͔ΒฒߦͰॲཧ͢Δ͜ͱ͕Ͱ͖ΔλεΫΛݟ͚ͭΔ • ฒߦԽʹͱ΋ͳ͏ΦʔόʔϔουΛθϩʹ͢Δͷ͸ݱ࣮తʹ͸೉͍͠ • ฒߦԽͷԸܙ͕ΦʔόʔϔουʹΑΔϖφϧςΟΛ্ճΕ͹ྑ͍