# A Micro-optimization You Will Never Need

DevFeed: [A Micro-optimization You Will Never Need](<https://devfeed.tech/articles/a-micro-optimization-you-will-never-need-25595.md>)

Original publisher: [Read original article](<https://www.romainguy.dev/posts/2024/a-micro-optimization-you-will-never-need/>)

Author: Romain Guy

Published: 2024-11-10T00: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>), [Code](<https://devfeed.tech/topics/code.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [assembly](<https://devfeed.tech/tags/assembly.md>), [code](<https://devfeed.tech/tags/code.md>), [compilation](<https://devfeed.tech/tags/compilation.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [developer](<https://devfeed.tech/tags/developer.md>), [devices](<https://devfeed.tech/tags/devices.md>), [graphics](<https://devfeed.tech/tags/graphics.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [low-power](<https://devfeed.tech/tags/low-power.md>), [optimization](<https://devfeed.tech/tags/optimization.md>), [performance](<https://devfeed.tech/tags/performance.md>)

## AI overview

This article presents a Kotlin micro-optimization that counts leading zeroes so comparisons can use smaller constants encoded directly in AArch64 instructions. The change reduces the compiled function from 21 to 18 instructions, with an estimated performance difference of about 0.5% on a Pixel 8 and around 11% on a Pixel 2.

## Source excerpt

Today I would like to show you a micro-optimization I recently used more for the fun of it than for its real impact. It's an interesting trick that you should never bother to use, nor worry about. It starts from a piece of Kotlin code that looked a bit like this (the original version used named constants, but I replaced them with their actual values for clarity in this context):