Skip to main content
  1. About
  2. For Teams
added 58 characters in body
Source Link

For the first time I tried to use Kotlin for something. Coming from python it was hard trying to figure out how Kotlin works. The way I got the time seems to not be a good way to measure it, giving a slightly different output everytime.

Code execution runtime:

~110 ms, best I got was 105ms

Approach:

I read the file line by line and count in a dictionary/map how many times a number has appeared.
Then the program goes through the list and notes how many times the number appears. At the end I go through the dictionary/map to see which number appeared the most.

I didn't make any big effort for the program to be fast.

Details about my pc:

Lenovo Legion 5
AMD Ryzen 5 5600H
32GiB of Ram
RTX 3060 Laptop

Code

import java.io.File
import java.io.InputStream
import java.util.Dictionary
import kotlin.reflect.typeOf
import kotlin.system.measureTimeMillis

fun main() {
    val time = measureTimeMillis {
        var thismap = mutableMapOf<Int, Int>()
        val inputStream: InputStream = File("1M_random_numbers.txt").inputStream()
        val lineList = mutableListOf<String>()

        inputStream.bufferedReader().forEachLine {
            if (it.toInt() in thismap) {
                var previous = thismap[it.toInt()]
                if (previous != null) {
                    thismap[it.toInt()] = 1 + previous
                }
            } else {
                thismap[it.toInt()] = 1
            }
            //if (it.toInt() in thismap) thismap[it.toInt()] = 1 + thismap[it.toInt()] else thismap[it.toInt()] = 1
        }
        var highestNumber = intArrayOf(0, 0)
        for (p in thismap) {
            if (p.value > highestNumber[0]) highestNumber = intArrayOf(p.value, p.key)
        }
        println(highestNumber.contentToString())
    }
    println(time)
}

For the first time I tried to use Kotlin for something. Coming from python it was hard trying to figure out how Kotlin works. The way I got the time seems to not be a good way to measure it, giving a slightly different output everytime.

Code execution runtime:

~110 ms, best I got was 105ms

Approach:

I read the file line by line and count in a dictionary/map how many times a number has appeared.
Then the program goes through the list and notes how many times the number appears. At the end I go through the dictionary/map to see which number appeared the most.

Details about my pc:

Lenovo Legion 5
AMD Ryzen 5 5600H
32GiB of Ram
RTX 3060 Laptop

Code

import java.io.File
import java.io.InputStream
import java.util.Dictionary
import kotlin.reflect.typeOf
import kotlin.system.measureTimeMillis

fun main() {
    val time = measureTimeMillis {
        var thismap = mutableMapOf<Int, Int>()
        val inputStream: InputStream = File("1M_random_numbers.txt").inputStream()
        val lineList = mutableListOf<String>()

        inputStream.bufferedReader().forEachLine {
            if (it.toInt() in thismap) {
                var previous = thismap[it.toInt()]
                if (previous != null) {
                    thismap[it.toInt()] = 1 + previous
                }
            } else {
                thismap[it.toInt()] = 1
            }
            //if (it.toInt() in thismap) thismap[it.toInt()] = 1 + thismap[it.toInt()] else thismap[it.toInt()] = 1
        }
        var highestNumber = intArrayOf(0, 0)
        for (p in thismap) {
            if (p.value > highestNumber[0]) highestNumber = intArrayOf(p.value, p.key)
        }
        println(highestNumber.contentToString())
    }
    println(time)
}

For the first time I tried to use Kotlin for something. Coming from python it was hard trying to figure out how Kotlin works. The way I got the time seems to not be a good way to measure it, giving a slightly different output everytime.

Code execution runtime:

~110 ms, best I got was 105ms

Approach:

I read the file line by line and count in a dictionary/map how many times a number has appeared.
Then the program goes through the list and notes how many times the number appears. At the end I go through the dictionary/map to see which number appeared the most.

I didn't make any big effort for the program to be fast.

Details about my pc:

Lenovo Legion 5
AMD Ryzen 5 5600H
32GiB of Ram
RTX 3060 Laptop

Code

import java.io.File
import java.io.InputStream
import java.util.Dictionary
import kotlin.reflect.typeOf
import kotlin.system.measureTimeMillis

fun main() {
    val time = measureTimeMillis {
        var thismap = mutableMapOf<Int, Int>()
        val inputStream: InputStream = File("1M_random_numbers.txt").inputStream()
        val lineList = mutableListOf<String>()

        inputStream.bufferedReader().forEachLine {
            if (it.toInt() in thismap) {
                var previous = thismap[it.toInt()]
                if (previous != null) {
                    thismap[it.toInt()] = 1 + previous
                }
            } else {
                thismap[it.toInt()] = 1
            }
            //if (it.toInt() in thismap) thismap[it.toInt()] = 1 + thismap[it.toInt()] else thismap[it.toInt()] = 1
        }
        var highestNumber = intArrayOf(0, 0)
        for (p in thismap) {
            if (p.value > highestNumber[0]) highestNumber = intArrayOf(p.value, p.key)
        }
        println(highestNumber.contentToString())
    }
    println(time)
}
Source Link

For the first time I tried to use Kotlin for something. Coming from python it was hard trying to figure out how Kotlin works. The way I got the time seems to not be a good way to measure it, giving a slightly different output everytime.

Code execution runtime:

~110 ms, best I got was 105ms

Approach:

I read the file line by line and count in a dictionary/map how many times a number has appeared.
Then the program goes through the list and notes how many times the number appears. At the end I go through the dictionary/map to see which number appeared the most.

Details about my pc:

Lenovo Legion 5
AMD Ryzen 5 5600H
32GiB of Ram
RTX 3060 Laptop

Code

import java.io.File
import java.io.InputStream
import java.util.Dictionary
import kotlin.reflect.typeOf
import kotlin.system.measureTimeMillis

fun main() {
    val time = measureTimeMillis {
        var thismap = mutableMapOf<Int, Int>()
        val inputStream: InputStream = File("1M_random_numbers.txt").inputStream()
        val lineList = mutableListOf<String>()

        inputStream.bufferedReader().forEachLine {
            if (it.toInt() in thismap) {
                var previous = thismap[it.toInt()]
                if (previous != null) {
                    thismap[it.toInt()] = 1 + previous
                }
            } else {
                thismap[it.toInt()] = 1
            }
            //if (it.toInt() in thismap) thismap[it.toInt()] = 1 + thismap[it.toInt()] else thismap[it.toInt()] = 1
        }
        var highestNumber = intArrayOf(0, 0)
        for (p in thismap) {
            if (p.value > highestNumber[0]) highestNumber = intArrayOf(p.value, p.key)
        }
        println(highestNumber.contentToString())
    }
    println(time)
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.