Performance

Let’s compare the following Kotlin snippet performing 40 million complex operations:

package org.kotlinmath.examples

import org.kotlinmath.*
import kotlin.system.measureTimeMillis

fun main() {
    val z = complex(2, 1)
    var w = ZERO
    val time = measureTimeMillis {
        repeat(10000000) {
            w += z
            w *= z
            w -= z
            w /= z
        }
    }
    println("Result: $w")
    println("Execution time: $time msec")
}

with the equivalent in Python:

import time

z = complex(2, 1)
w = complex(0, 0)
start = time.time()
for i in range(0, 10000000):
    w += z
    w *= z
    w -= z
    w /= z
ende = time.time()

print("Result: ", w)
print("Execution time: ", ende - start, "sec")

And the winner is ……
Kotlin with an approximately ten times faster execution time!

Kotlin:
Result: 1.0E7+1.0E7i
Execution time: 258 msec

Python:
Result: (10000000+10000000j)
Execution time: 2.7665789127349854 sec

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.