# Avoiding Compiler Elimination in Code Microbenchmarks

DevFeed: [Avoiding Compiler Elimination in Code Microbenchmarks](<https://devfeed.tech/articles/you-are-going-to-need-it-25608.md>)

Original publisher: [Read original article](<https://www.romainguy.dev/posts/2024/you-are-going-to-need-it/>)

Author: Romain Guy

Published: 2024-12-13T00:00:00Z

Content type: tutorial

Language: en

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

Topics: [benchmarking](<https://devfeed.tech/topics/benchmarking.md>), [Code](<https://devfeed.tech/topics/code.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>), [Assembly](<https://devfeed.tech/topics/assembly.md>), [Benchmark](<https://devfeed.tech/topics/benchmark.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [assembly](<https://devfeed.tech/tags/assembly.md>), [benchmark](<https://devfeed.tech/tags/benchmark.md>), [benchmarking](<https://devfeed.tech/tags/benchmarking.md>), [benchmarks](<https://devfeed.tech/tags/benchmarks.md>), [code](<https://devfeed.tech/tags/code.md>), [compare](<https://devfeed.tech/tags/compare.md>), [compiler](<https://devfeed.tech/tags/compiler.md>), [developer](<https://devfeed.tech/tags/developer.md>), [graphics](<https://devfeed.tech/tags/graphics.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [measurement](<https://devfeed.tech/tags/measurement.md>), [performance](<https://devfeed.tech/tags/performance.md>)

## AI overview

This article explains how compiler and runtime optimizations can eliminate the computation being measured in a microbenchmark. Using a comparison between value.pow(2f) and value * value, it shows why inspecting generated code and introducing a carefully placed side effect are necessary for a valid measurement.

## Source excerpt

Optimizing code can be a difficult task because there are so many traps you need to avoid at every step of the way. Today I want to focus on one of the (numerous) benchmarking traps, which you may have run into, and that I myself encounter regularly. Let's imagine you are trying to optimize code, and you notice the use of a value.pow(2f). One obvious way to optimize this is to replace the function call with a multiplication (value * value), but is it worth it? Since you are a diligent engineer, you decide to write a microbenchmark to compare before and after: