xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

算法的时间复杂度 & 性能对比

累加算法性能对比

// js 累加算法性能对比测试
const n = 10**6;

(() => {
  console.time(`for`);
  let result = 0;
  for (let i = 1; i <= n; i++) {
    result += i;
    if (i === n) {
      console.log(`ok`, result);
    }
  }
  console.timeEnd(`for`);
})();

(() => {
  console.time(`math`);
  const result = (n * (n + 1)) / 2;
  console.log(`ok`, result);
  console.timeEnd(`math`);
})();

如何计算算法的复杂度

https://time.geekbang.org/course/detail/100019701-41531

refs

https://www.bigocheatsheet.com/

Big-O Complexity Chart

O(n^2) O(n log(n)) O(log(n)) O(n)

Ω(n^2 ) Ω(n log(n)) Ω(log(n)) Ω(n)

Θ(n^2 ) Θ(n log(n)) Θ(log(n)) Θ(n))


Flag Counter

©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


原文地址:https://www.cnblogs.com/xgqfrms/p/13491038.html