From 3f93f2796da1aa7044563b5553872b8fdd2f13e0 Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Fri, 15 Apr 2022 07:40:10 +0000 Subject: [PATCH] [K/N] Add ComplexArraysBenchmark Merge-request: KT-MR-6082 Merged-by: Alexander Shabalin --- .../performance/ring/src/main/kotlin/main.kt | 3 +- .../jetbrains/ring/ComplexArraysBenchmark.kt | 59 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 kotlin-native/performance/ring/src/main/kotlin/org/jetbrains/ring/ComplexArraysBenchmark.kt diff --git a/kotlin-native/performance/ring/src/main/kotlin/main.kt b/kotlin-native/performance/ring/src/main/kotlin/main.kt index 1554cc25554..74a0a1dd0ff 100644 --- a/kotlin-native/performance/ring/src/main/kotlin/main.kt +++ b/kotlin-native/performance/ring/src/main/kotlin/main.kt @@ -223,7 +223,8 @@ class RingLauncher : Launcher() { "Calls.interfaceMethodBimorphic" to BenchmarkEntryWithInit.create(::CallsBenchmark, { interfaceMethodCall_BimorphicCallsite() }), "Calls.interfaceMethodTrimorphic" to BenchmarkEntryWithInit.create(::CallsBenchmark, { interfaceMethodCall_TrimorphicCallsite() }), "Calls.interfaceMethodHexamorphic" to BenchmarkEntryWithInit.create(::CallsBenchmark, { interfaceMethodCall_HexamorphicCallsite() }), - "LocalObjects.localArray" to BenchmarkEntryWithInit.create(::LocalObjectsBenchmark, { localArray() }) + "LocalObjects.localArray" to BenchmarkEntryWithInit.create(::LocalObjectsBenchmark, { localArray() }), + "ComplexArrays.outerProduct" to BenchmarkEntryWithInit.create(::ComplexArraysBenchmark, { outerProduct() }), ) } diff --git a/kotlin-native/performance/ring/src/main/kotlin/org/jetbrains/ring/ComplexArraysBenchmark.kt b/kotlin-native/performance/ring/src/main/kotlin/org/jetbrains/ring/ComplexArraysBenchmark.kt new file mode 100644 index 00000000000..5e3cc349794 --- /dev/null +++ b/kotlin-native/performance/ring/src/main/kotlin/org/jetbrains/ring/ComplexArraysBenchmark.kt @@ -0,0 +1,59 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +package org.jetbrains.ring + +import org.jetbrains.benchmarksLauncher.Blackhole +import org.jetbrains.benchmarksLauncher.Random + +// Benchmark is inspired by multik library. + +private class ComplexDouble(public val re: Double, public val im: Double) { + public operator fun plus(other: ComplexDouble): ComplexDouble = ComplexDouble(re + other.re, im + other.im) + + public operator fun times(other: ComplexDouble): ComplexDouble = + ComplexDouble(re * other.re - im * other.im, re * other.im + other.re * im) +} + +private class ComplexDoubleArray(public val size: Int) { + private val data: DoubleArray = DoubleArray(size * 2) + + public operator fun get(index: Int): ComplexDouble { + val i = index shl 1 + return ComplexDouble(data[i], data[i + 1]) + } + + public operator fun set(index: Int, value: ComplexDouble): Unit { + val i = index shl 1 + data[i] = value.re + data[i + 1] = value.im + } +} + +open class ComplexArraysBenchmark { + private val size = 1000 + private val a = ComplexDoubleArray(size) + private val b = ComplexDoubleArray(size) + + init { + for (i in 0 until size) { + a[i] = ComplexDouble(Random.nextDouble(), Random.nextDouble()) + b[i] = ComplexDouble(Random.nextDouble(), Random.nextDouble()) + } + } + + //Benchmark + fun outerProduct() { + val result = ComplexDoubleArray(size * size) + + for (i in 0 until size) { + for (j in 0 until size) { + result[i + j * size] += a[i] * b[j] + } + } + + Blackhole.consume(result) + } +}