Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance #2

Merged
merged 1 commit into from
Oct 21, 2021
Merged

Improve performance #2

merged 1 commit into from
Oct 21, 2021

Conversation

uhooi
Copy link
Contributor

@uhooi uhooi commented Oct 18, 2021

概要

LegoColor(r:g:b:) の処理に時間がかかるため、rgb の値をキーとしてキャッシュするようにしました。

私のアイコンを StudType: RoundMax Stud: 128 で試したところ、処理時間が約 1/33 になりました。
同じ色を使っている画像ほど処理が速くなります。
逆にほぼ異なる色だと、キャッシュを捜査する分だけ処理が遅くなりますが、具体的な時間は計測していません。

処理時間の計測は colorMap 生成の前後に以下を仕込んで行いました。

+       let startDate = Date()
        let colorMap = (0 ..< Int(size.width * size.height))
            .map { i -> LegoColor in
                let r = CGFloat(rgbaData[4 * i]) / 255.0
                let g = CGFloat(rgbaData[4 * i + 1]) / 255.0
                let b = CGFloat(rgbaData[4 * i + 2]) / 255.0
                return LegoColor(r: r, g: g, b: b)
            }
+       print("Time: \(Date().timeIntervalSince(startDate))")

Before

Time
2.201616048812866
2.205173969268799
2.2091729640960693
2.2038110494613647
2.204527974128723

After

time
0.06664800643920898
0.06678497791290283
0.06787693500518799
0.06665492057800293
0.06649196147918701

スクリーンショット 2021-10-18 20 35 40

@uhooi
Copy link
Contributor Author

uhooi commented Oct 18, 2021

以下の画像で試したところ、Before と After で処理時間がほぼ変わりませんでした。
そのため本 PR でパフォーマンスが向上することがあっても、極端に低下することはなさそうです。

https://imgur.com/a/O9OyA
https://i.imgur.com/wvKAQUvh.jpg
wvKAQUvh

@Kyome22 Kyome22 merged commit 3ef9d3c into Kyome22:main Oct 21, 2021
@Kyome22
Copy link
Owner

Kyome22 commented Oct 21, 2021

コミットありがとうございます!
さらに軽量化の最適化に挑戦してみます。

@uhooi uhooi deleted the feature/improve_performance branch October 21, 2021 06:56
@uhooi
Copy link
Contributor Author

uhooi commented Oct 21, 2021

ぜひぜひ!
キーとなる RGB の組み合わせを減らせば、さらに高速になります!

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.

None yet

2 participants