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

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.

主站蜘蛛池模板: 新津县| 德钦县| 天祝| 抚松县| 本溪市| 金平| 新乡县| 广平县| 黑龙江省| 久治县| 丹巴县| 东台市| 新和县| 临邑县| 鸡东县| 错那县| 新余市| 彝良县| 平山县| 双鸭山市| 平南县| 东乡族自治县| 桐乡市| 惠安县| 湖北省| 尚志市| 尚义县| 盘山县| 宁陵县| 周口市| 苍南县| 河北区| 榆社县| 巴林左旗| 文水县| 桑日县| 济阳县| 临洮县| 沁源县| 滦平县| 新郑市|