# The Path Not Taken

DevFeed: [The Path Not Taken](<https://devfeed.tech/articles/the-path-not-taken-25607.md>)

Original publisher: [Read original article](<https://www.romainguy.dev/posts/2024/the-path-not-taken/>)

Author: Romain Guy

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

Content type: article

Language: en

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

Topics: [Benchmark](<https://devfeed.tech/topics/benchmark.md>), [Algorithm](<https://devfeed.tech/topics/algorithm.md>), [cpu](<https://devfeed.tech/topics/cpu.md>), [Compiler](<https://devfeed.tech/topics/compiler.md>)

Tags: [algorithm](<https://devfeed.tech/tags/algorithm.md>), [android](<https://devfeed.tech/tags/android.md>), [benchmark](<https://devfeed.tech/tags/benchmark.md>), [benchmarks](<https://devfeed.tech/tags/benchmarks.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>), [performance](<https://devfeed.tech/tags/performance.md>)

## AI overview

This article explains how benchmark data patterns can produce misleading performance results. Ordered pixel values allow branch prediction to work almost perfectly, while randomized values cause many branch misses and make the benchmark about 2.3 times slower. It then shows how a branchless implementation can restore comparable performance for randomized data on Android.

## Source excerpt

In the last post, we saw that benchmarks don't always measure what we think they measure. Let's look at another instance of this problem today, starting with this rather simple benchmark: 1@RunWith(AndroidJUnit4::class) 2class DataBenchmark { 3 @get:Rule 4 val benchmarkRule = BenchmarkRule() 5 6 // Generate data in [0..255] 7 private val data = IntArray(65_536) { 8 it % 256 9 } 10 11 @Test 12 fun processData() { 13 var sum = 0f 14 benchmarkRule.measureRepeated { 15 for (d in data) { 16 if (d < 128) { 17 sum += d / 128f 18 } 19 } 20 } 21 BlackHole.consume(sum) 22 } 23} This benchmark tests an algorithm that processes an array of values between 0 and 255 (8-bit pixels for instance), and computes the sum of the normalized values for pixels less than 128.