# Optimization, Step by Step

DevFeed: [Optimization, Step by Step](<https://devfeed.tech/articles/optimization-step-by-step-25603.md>)

Original publisher: [Read original article](<https://www.romainguy.dev/posts/2024/optimization-step-by-step/>)

Author: Romain Guy

Published: 2024-11-25T00:00:00Z

Content type: tutorial

Language: en

Sources: [Posts on Romain Guy](<https://devfeed.tech/sources/posts-on-romain-guy.md>)

Topics: [Optimization](<https://devfeed.tech/topics/optimization.md>), [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Android](<https://devfeed.tech/topics/android.md>), [pixel](<https://devfeed.tech/topics/pixel.md>), [Algorithm](<https://devfeed.tech/topics/algorithm.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [android](<https://devfeed.tech/tags/android.md>), [android-14](<https://devfeed.tech/tags/android-14.md>), [code](<https://devfeed.tech/tags/code.md>), [developer](<https://devfeed.tech/tags/developer.md>), [graphics](<https://devfeed.tech/tags/graphics.md>), [implementation](<https://devfeed.tech/tags/implementation.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [performance](<https://devfeed.tech/tags/performance.md>), [pixel](<https://devfeed.tech/tags/pixel.md>)

## AI overview

This tutorial describes step-by-step optimizations to the Kotlin implementation of BlurHash decoding. On a Pixel 6 running Android 14, the changes made decoding a fixed blur hash into a 384x384 image up to 4.35 times faster; one optimization using direct character access and a lookup table improved performance by roughly 5%.

## Source excerpt

BlurHash is a compact representation of placeholders for images. A blur hash is encoded as a short string that can be rendered to a bitmap at runtime to display a "blurry" version of the source image. The way it works remind me of how spherical harmonics are used in 3D rendering engines to efficiently encode irradiance. I recently remembered that I had been meaning to look at the Kotlin implementation of BlurHash to see if there was a way to make it faster. I looked up the KMP port and after a few changes, I was able to make decoding a blur hash up to 4.35x faster on a Pixel 6 running Android 14. For all the measurements mentioned in this article, I decoded a fixed blur hash to a 384x384 image. The original code took 24.4 ms to do this when running at full speed, and 41.5 ms when locking the CPU clocks. This is way too long considering the low resolution of the image.