list

The Average Case assumes parameters generated uniformly at random.

Internally, a list is represented as an array; the largest costs come from growing beyond the current allocation size (because everything must move), or from inserting or deleting somewhere near the beginning (because everything after that must move). If you need to add/remove at both ends, consider using a collections.deque instead.

Operation

Average Case

Amortized Worst Case

Copy

O(n)

O(n)

Append[1]

O(1)

O(1)

Insert

O(n)

O(n)

Get Item

O(1)

O(1)

Set Item

O(1)

O(1)

Delete Item

O(n)

O(n)

Iteration

O(n)

O(n)

Get Slice

O(k)

O(k)

Del Slice

O(n)

O(n)

Set Slice

O(k+n)

O(k+n)

Extend[1]

O(k)

O(k)

Sort

O(n log n)

O(n log n)

Multiply

O(nk)

O(nk)

x in s

O(n)

min(s), max(s)

O(n)

Get Length

O(1)

O(1)

collections.deque

A deque (double-ended queue) is represented internally as a doubly linked list. (Well, a list of arrays rather than objects, for greater efficiency.) Both ends are accessible, but even looking at the middle is slow, and adding to or removing from the middle is slower still.

Operation

Average Case

Amortized Worst Case

Copy

O(n)

O(n)

append

O(1)

O(1)

appendleft

O(1)

O(1)

pop

O(1)

O(1)

popleft

O(1)

O(1)

extend

O(k)

O(k)

extendleft

O(k)

O(k)

rotate

O(k)

O(k)

remove

O(n)

O(n)

set

See dict -- the implementation is intentionally very similar.

Operation

Average case

Worst Case

x in s

O(1)

O(n)

Union s|t

O(len(s)+len(t))

Intersection s&t

O(min(len(s), len(t))

O(len(s) * len(t))

Difference s-t

O(len(s))

s.difference_update(t)

O(len(t))

Symmetric Difference s^t

O(len(s))

O(len(s) * len(t))

s.symmetric_difference_update(t)

O(len(t))

O(len(t) * len(s))

  • As seen in the source code the complexities for set difference s-t or s.difference(t) (set_difference()) and in-place set difference s.difference_update(t) (set_difference_update_internal()) are different! The first one is O(len(s)) (for every element in s add it to the new set, if not in t). The second one is O(len(t)) (for every element in t remove it from s). So care must be taken as to which is preferred, depending on which one is the longest set and whether a new set is needed.

  • To perform set operations like s-t, both s and t need to be sets. However you can do the method equivalents even if t is any iterable, for example s.difference(l), where l is a list.

dict

The Average Case times listed for dict objects assume that the hash function for the objects is sufficiently robust to make collisions uncommon. The Average Case assumes the keys used in parameters are selected uniformly at random from the set of all keys.

Note that there is a fast-path for dicts that (in practice) only deal with str keys; this doesn't affect the algorithmic complexity, but it can significantly affect the constant factors: how quickly a typical program finishes.

Operation

Average Case

Amortized Worst Case

Copy[2]

O(n)

O(n)

Get Item

O(1)

O(n)

Set Item[1]

O(1)

O(n)

Delete Item

O(1)

O(n)

Iteration[2]

O(n)

O(n)


最新文章

  1. 手写一个allocator
  2. python 处理视频输入输出
  3. 【总结】详细说说@Html.ActionLink()的用法
  4. 从oracle数据库中导出excel问题导致乱码的问题
  5. css3过渡
  6. 15.linux 中无法输入指令
  7. AJAX XML返回类型
  8. 兼容性好的CSS字体投影
  9. 【Todo】网络编程学习-面向工资编程
  10. 初学structs2,表单验证
  11. phpcms v9 源码解析(4)content模块下的index.php文件的init()方法解析
  12. 奇货商城重构——webpack自动化工程
  13. js bind0
  14. 18.13 Uboot分析与移植
  15. follow
  16. chrome 浏览器之下载管理器插件
  17. for(var i=1;i<=3;i++){ setTimeout(function(){ console.log(i); },0); };答案:4 4 4。
  18. HDU 1166 敌兵布阵(线段树点更新区间求和裸题)
  19. jsonrpc使用
  20. RCU介绍

热门文章

  1. jsp利用application统计在线人数的方法
  2. bzoj4498: 魔法的碰撞
  3. String()与 toString()
  4. js获取Html元素的实际宽度高度
  5. eclipse注释快捷键(含方法注释)
  6. Android实现透明式状态栏
  7. 11月7日下午PHP----PDO访问方式操作数据库
  8. Google 地图 API V3 之事件
  9. Android 签名证书
  10. 4Struts2标签库----青软S2SH(笔记)