Skip to content

Kotlin library that can benchmark your code snippets with beautiful console output.

License

Notifications You must be signed in to change notification settings

nyabkun/qq-benchmark

Repository files navigation

🐕 qq-benchmark

qq-benchmark is a Kotlin library that can benchmark your code snippets with beautiful console output.

How to use

Example

output

result.png

code example

Full Source : QBenchmarkExample.kt

qBenchmark {
    // Number of trials
    nTry = 500

    // If this instance has several [block]s, it will shuffle them in randomized order and measure the time.
    // [nSingleMeasureLoop] represents how many times a block is executed in one measurement.
    // Eventually, the code snippet in the [block] will be executed [nSingleMeasureLoop] * [nTry] times.
    nSingleMeasureLoop = 5

    // In the early executions, the execution of the [block] takes more time,
    // so we first perform some executions which are not counted in the measurements.
    nWarmUpTry = 50

    block("Raw String Concat") {
        var str = ""
        for (i in 1..3000) {
            str += i.toString()
        }
        str
    }

    block("StringBuilder") {
        val sb = StringBuilder()
        for (i in 1..3000) {
            sb.append(i.toString())
        }
        sb.toString()
    }

    block("StringBuffer") {
        val sb = StringBuffer()
        for (i in 1..3000) {
            sb.append(i.toString())
        }
        sb.toString()
    }
}

Please see QBenchmarkTest.kt for more code examples. Single-File version src-test-single/QBenchmarkTest.kt is a self-contained source code that includes a runnable main function. You can easily copy and paste it into your codebase.

Public API

Single-File version Dependency

If you copy & paste QBenchmark.kt, fefer to build.gradle.kts to directly check project settings.

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-reflect:1.8.20")
    implementation("org.jetbrains.kotlin:kotlin-stdlib:1.8.20")
}

Jar version Maven Dependency

If you prefer a jar library, you can use jitpack.io repository.

build.gradle ( Groovy )

repositories {
    ...
    maven { url 'https://jitpack.io' }
}

dependencies {
    implementation 'com.github.nyabkun:qq-benchmark:v2023-05-28'
}

build.gradle.kts ( Kotlin )

repositories {
    ...
    maven("https://jitpack.io")
}

dependencies {
    implementation("com.github.nyabkun:qq-benchmark:v2023-05-28")
}

pom.xml

<repositories>
    ...
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependencies>
    ...
    <dependency>
        <groupId>com.github.nyabkun</groupId>
        <artifactId>qq-benchmark</artifactId>
        <version>v2023-05-28</version>
    </dependency>
</dependencies>

How did I create this library

  • This library was created using qq-compact-lib to generates a compact, self-contained library.
  • qq-compact-lib is a Kotlin library that can extract code elements from your codebase and make a compact library.
  • It utilizes PSI to resolve function calls and class references.
  • The original repository is currently being organized, and I'm gradually extracting and publishing smaller libraries.

About

Kotlin library that can benchmark your code snippets with beautiful console output.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages