官术网_书友最值得收藏!

  • Learning Concurrency in Kotlin
  • Miguel Angel Castiblanco Torres
  • 200字
  • 2021-08-05 10:46:41

CPU-bound

Many algorithms are built around operations that need only the CPU to be completed. The performance of such algorithms will be delimited by the CPU in which they are running, and solely upgrading the CPU will usually improve their performance.

Let's think, for example, of a simple algorithm that takes a word and returns whether it's a palindrome or not:

fun isPalindrome(word: String) : Boolean {
val lcWord = word.toLowerCase()
return lcWord == lcWord.reversed()
}

Now, let's consider that this function is called from another function, filterPalindromes(), which takes a list of words and returns the ones that are palindromes:

fun filterPalindromes(words: List<String>) : List<String> {
return words.filter { isPalindrome(it) }
}

Finally, filterPalindromes() is called from the main method of the application where a list of words has been already defined:

val words = listOf("level", "pope", "needle", "Anna", "Pete", "noon", "stats")

fun main(args: Array<String>) {
filterPalindromes(words).forEach {
println(it)
}
}

In this example, all the parts of the execution depend on the CPU. If the code is updated to send hundreds of thousands of words, filterPalindromes() will take longer. Also, if the code is executed in a faster CPU, the performance will be improved without code changes.

主站蜘蛛池模板: 宜都市| 固镇县| 阿勒泰市| 新建县| 鸡东县| 安阳市| 澄城县| 荔浦县| 常山县| 湖口县| 嵊州市| 闽清县| 和龙市| 龙川县| 资溪县| 阳西县| 诸暨市| 孝义市| 台东市| 襄城县| 安丘市| 洪洞县| 锡林浩特市| 铅山县| 阿鲁科尔沁旗| 敦化市| 乐亭县| 天柱县| 夏津县| 景谷| 秭归县| 康乐县| 辉南县| 富顺县| 靖西县| 克什克腾旗| 大姚县| 卓资县| 修水县| 竹溪县| 筠连县|