- 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.
- UI設(shè)計基礎(chǔ)培訓(xùn)教程
- Cocos2D-X權(quán)威指南(第2版)
- Redis Applied Design Patterns
- Python自動化運維快速入門(第2版)
- Hands-On Automation Testing with Java for Beginners
- Android開發(fā)案例教程與項目實戰(zhàn)(在線實驗+在線自測)
- 輕松上手2D游戲開發(fā):Unity入門
- GameMaker Essentials
- 快速入門與進階:Creo 4·0全實例精講
- Unity Character Animation with Mecanim
- 深入解析Java編譯器:源碼剖析與實例詳解
- 邊玩邊學(xué)Scratch3.0少兒趣味編程
- 計算機應(yīng)用基礎(chǔ)案例教程(第二版)
- Python滲透測試編程技術(shù):方法與實踐(第2版)
- Unity 3D UI Essentials