zoukankan      html  css  js  c++  java
  • 一道求质数的题,不同的算法体现不同的程序性能。

    输出1~10000之间素数,编写一个返回是否为质数的程序。

    1.用时最长的函数 在2.0GB处理器的运算时间 >19s。
    这个性能最差,编写者认为循环测试需要被测试n/2 - 1次。可见没有基本数学知识的掌握,在编写代码时是多么可怕,19s啊!
    Code

    2.一般的性能的函数 在2.0GB处理器的运输时间>=3s
    这是一般性能的函数,编写代码者考虑到了使用sqrt(n)作为被测试的次数。
    Code

    3.性能最佳的函数 在2.0GB处理器的运输时间>=2s
    这是性能最好的函数。一旦sum有值,即一旦参数在被测试条件成立时被除尽,立即退出函数。这样接下来的数就不用测试了。
    Code

    不知道还有没有比这个性能还好的函数?

  • 相关阅读:
    [NPM] Avoid Duplicate Commands by Calling one NPM Script from Another
    [Algorithm] Dynamic programming: Find Sets Of Numbers That Add Up To 16
    [React] Refactor a Class Component with React hooks to a Function
    [Algorithm] Construct a Binary Tree and Binary Search
    设计模式(装饰者模式)
    IOS设计模式之二(门面模式,装饰器模式)
    IOS设计模式之三(适配器模式,观察者模式)
    linux内核源码阅读之facebook硬盘加速flashcache之五
    IOS设计模式之四(备忘录模式,命令模式)
    DRP总结
  • 原文地址:https://www.cnblogs.com/iPeterRex/p/1244463.html
Copyright © 2011-2022 走看看