跳转至内容
  • 社区首页
  • 版块
  • 最新
  • 标签
  • 热门
折叠

GitHub中文论坛

  1. 主页
  2. 版块
  3. 开源分享
  4. 关于自己写的和时间复杂度有关的代码和测试/python

关于自己写的和时间复杂度有关的代码和测试/python

已定时 已固定 已锁定 已移动 开源分享
2 帖子 2 发布者 603 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
回复
  • 在新帖中回复
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • qinlang99999Q 离线
    qinlang99999Q 离线
    qinlang99999
    写于 最后由 编辑
    #1

    import matplotlib.pylab as plt
    import numpy as np
    import math
    import statistics
    import timeit
    import random
    #%matplotlib inline
    #1,O(N)时的代码

    ns = [i for i in range(0,100,2)]
    ts = [timeit.timeit('f1({})'.format([i for i in range(n)]), number=10000, globals=globals()) for n in ns]

    plt.plot(ns, ts, 'or')

    avg_slope = statistics.mean((ts[i+1] - ts[i]) / (ns[i+1] - ns[i]) for i in range(len(ns)-1))

    plt.plot(ns, [1.2avg_slopen for n in ns], '-b')
    plt.plot(ns, [0.8avg_slopen for n in ns], '-g');
    #2,O(logn)时的代码

    ns = [i for i in range(0,100,2)]
    ts = [timeit.timeit('f2({})'.format(n), number=1000, globals=globals()) for n in ns]

    plt.plot(ns, ts, 'or')
    avg_slope = 0.4statistics.mean((ts[i+1] - ts[i]) / (math.log(ns[i+1]+1,2) - math.log(ns[i]+1,2)) for i in range(len(ns)-1))
    plt.plot(ns, [1.2
    abs(avg_slopemath.log(n+1,2)) for n in ns], '-b')
    plt.plot(ns, [0.8
    abs(avg_slope*math.log(n+1,2)) for n in ns], '-g')

    #3,O(N)代码的列表形式
    ns = [i for i in range(0,100,2)]
    ts = [timeit.timeit('f3({})'.format([i for i in range(n)]), number=10000, globals=globals()) for n in ns]

    plt.plot(ns, ts, 'or')

    avg_slope = statistics.mean((ts[i+1] - ts[i]) / (ns[i+1] - ns[i]) for i in range(len(ns)-1))

    plt.plot(ns, [1.2avg_slopen for n in ns], '-b')
    plt.plot(ns, [0.8avg_slopen for n in ns], '-g');
    #4,O(e^n)的代码

    ns = [i for i in range(0,20,2)]
    ts = [timeit.timeit('f4({})'.format(n), number=1, globals=globals()) for n in ns]

    plt.plot(ns, ts, 'or')

    avg_slope = 0.1*statistics.mean((ts[i+1] -ts[i]) / (math.exp(ns[i+1]) - math.exp(ns[i])) for i in range(len(ns)-1))

    plt.plot(ns, [0.8avg_slopemath.exp(n) for n in ns], '-b')
    plt.plot(ns, [1.2avg_slopemath.exp(n) for n in ns], '-b')
    #5,O(sqrt(n))的代码

    ns = [i for i in range(0,100,2)]
    ts = [timeit.timeit('f5({})'.format([i for i in range(n)]), number=100, globals=globals()) for n in ns]

    plt.plot(ns, ts, 'or')
    avg_slope = 5*statistics.mean((ts[i+1] - ts[i]) / (ns[i+1] - ns[i]) for i in range(len(ns)-1))

    plt.plot(ns, [1.2avg_slopemath.sqrt(n) for n in ns], '-b')
    plt.plot(ns, [0.8avg_slopemath.sqrt(n) for n in ns], '-b')

    1 条回复 最后回复
    0
    • ZzzmllZ 离线
      ZzzmllZ 离线
      Zzzmll
      写于 最后由 编辑
      #2

      你们是真的会,新手真的秃头了

      1 条回复 最后回复
      0
      回复
      • 在新帖中回复
      登录后回复
      • 从旧到新
      • 从新到旧
      • 最多赞同


      • 登录

      • 第一个帖子
        最后一个帖子
      0
      • 社区首页
      • 版块
      • 最新
      • 标签
      • 热门