https://pypi.org/project/blist/

blist: an asymptotically faster list-like type for Python — blist 1.3.6 documentation http://stutzbachenterprises.com/blist/

The blist is a drop-in replacement for the Python list that provides better performance when modifying large lists. The blist package also provides sortedlistsortedsetweaksortedlistweaksortedsetsorteddict, and btuple types.

Full documentation is at the link below:

http://stutzbachenterprises.com/blist-doc/

Python’s built-in list is a dynamically-sized array; to insert or remove an item from the beginning or middle of the list, it has to move most of the list in memory, i.e., O(n) operations. The blist uses a flexible, hybrid array/tree structure and only needs to move a small portion of items in memory, specifically using O(log n) operations.

For small lists, the blist and the built-in list have virtually identical performance.

To use the blist, you simply change code like this:

>>> items = [5, 6, 2]
>>> more_items = function_that_returns_a_list()

to:

>>> from blist import blist
>>> items = blist([5, 6, 2])
>>> more_items = blist(function_that_returns_a_list())

Here are some of the use cases where the blist asymptotically outperforms the built-in list:

Use Case blist list
Insertion into or removal from a list O(log n) O(n)
Taking slices of lists O(log n) O(n)
Making shallow copies of lists O(1) O(n)
Changing slices of lists O(log n + log k) O(n+k)
Multiplying a list to make a sparse list O(log k) O(kn)
Maintain a sorted lists with bisect.insort O(log**2 n) O(n)

So you can see the performance of the blist in more detail, several performance graphs available at the following link: http://stutzbachenterprises.com/blist/

Example usage:

>>> from blist import *
>>> x = blist([0]) # x is a blist with one element
>>> x *= 2**29 # x is a blist with > 500 million elements
>>> x.append(5) # append to x
>>> y = x[4:-234234] # Take a 500 million element slice from x
>>> del x[3:1024] # Delete a few thousand elements from x

Other data structures

The blist package provides other data structures based on the blist:

  • sortedlist
  • sortedset
  • weaksortedlist
  • weaksortedset
  • sorteddict
  • btuple

These additional data structures are only available in Python 2.6 or higher, as they make use of Abstract Base Classes.

The sortedlist is a list that’s always sorted. It’s iterable and indexable like a Python list, but to modify a sortedlist the same methods you would use on a Python set (add, discard, or remove).

>>> from blist import sortedlist
>>> my_list = sortedlist([3,7,2,1])
>>> my_list
sortedlist([1, 2, 3, 7])
>>> my_list.add(5)
>>> my_list[3]
5
>>>

The sortedlist constructor takes an optional “key” argument, which may be used to change the sort order just like the sorted() function.

>>> from blist import sortedlist
>>> my_list = sortedlist([3,7,2,1], key=lambda i: -i)
sortedlist([7, 3, 2, 1]
>>>

The sortedset is a set that’s always sorted. It’s iterable and indexable like a Python list, but modified like a set. Essentially, it’s just like a sortedlist except that duplicates are ignored.

>>> from blist import sortedset
>>> my_set = sortedset([3,7,2,2])
sortedset([2, 3, 7]
>>>

The weaksortedlist and weaksortedset are weakref variations of the sortedlist and sortedset.

The sorteddict works just like a regular dict, except the keys are always sorted. The sorteddict should not be confused with Python 2.7’s OrderedDict type, which remembers the insertion order of the keys.

>>> from blist import sorteddict
>>> my_dict = sorteddict({1: 5, 6: 8, -5: 9})
>>> my_dict.keys()
[-5, 1, 6]
>>>

The btuple is a drop-in replacement for the built-in tuple. Compared to the built-in tuple, the btuple offers the following advantages:

  • Constructing a btuple from a blist takes O(1) time.
  • Taking a slice of a btuple takes O(n) time, where n is the size of the original tuple. The size of the slice does not matter.
>>> from blist import blist, btuple
>>> x = blist([0]) # x is a blist with one element
>>> x *= 2**29 # x is a blist with > 500 million elements
>>> y = btuple(x) # y is a btuple with > 500 million elements

Installation instructions

Python 2.5 or higher is required. If building from the source distribution, the Python header files are also required. In either case, just run:

python setup.py install

If you’re running Linux and see a bunch of compilation errors from GCC, you probably do not have the Python header files installed. They’re usually located in a package called something like “python2.6-dev”.

The blist package will be installed in the ‘site-packages’ directory of your Python installation. (Unless directed elsewhere; see the “Installing Python Modules” section of the Python manuals for details on customizing installation locations, etc.).

If you downloaded the source distribution and wish to run the associated test suite, you can also run:

python setup.py test

which will verify the correct installation and functioning of the package. The tests require Python 2.6 or higher.

Feedback

We’re eager to hear about your experiences with the blist. You can email me at daniel@stutzbachenterprises.com. Alternately, bug reports and feature requests may be reported on our bug tracker at: http://github.com/DanielStutzbach/blist/issues

How we test

In addition to the tests include in the source distribution, we perform the following to add extra rigor to our testing process:

  1. We use a “fuzzer”: a program that randomly generates list operations, performs them using both the blist and the built-in list, and compares the results.
  2. We use a modified Python interpreter where we have replaced the array-based built-in list with the blist. Then, we run all of the regular Python unit tests.

O(n) O(log n) blist: an asymptotically faster list-like type for Python的更多相关文章

  1. 自动统计安卓log中Anr,Crash,Singnal出现数量的Python脚本 (转载)

    自动统计安卓log中Anr,Crash,Singnal出现数量的Python脚本   转自:https://www.cnblogs.com/ailiailan/p/8304989.html 作为测试, ...

  2. 自动统计安卓log中Anr,Crash,Singnal出现数量的Python脚本

    作为测试,在测试工作中一定会经常抓log,有时log收集时间很长,导致log很大,可能达到几G,想找到能打开如此大的log文件的工具都会变得困难:即使log不大时,我们可以直接把log发给开发同学去分 ...

  3. 脚本自动统计安卓log中Anr、Crash等出现的数量(Python)

    作为测试,在测试工作中一定会经常抓log,有时log收集时间很长,导致log很大,可能达到几G,想找到能打开如此大的log文件的工具都会变得困难:即使log不大时,我们可以直接把log发给开发同学去分 ...

  4. Mininet在创建拓扑的过程中为什么不打印信息了——了解Mininet的log系统

    前言 写这篇博客是为了给我的愚蠢和浪费的6个小时买单! 过程原因分析 我用Mininet创建过不少拓扑了,这次创建的拓扑非常简单,如下图,创建拓扑的代码见github.在以前的拓扑创建过程中,我都是用 ...

  5. openstack 中 log模块分析

    1 . 所在模块,一般在openstack/common/log.py,其实最主要的还是调用了python中的logging模块: 入口函数在 def setup(product_name, vers ...

  6. Log4Net .NET log处理

    1.NuGet 安装Log4Net. 2.新建一个Common的project,并且添加一个LogWriter的类: public class LogWriter { //Error log publ ...

  7. python 每日一练: 读取log文件中的数据,并画图表

    之前在excel里面分析log数据,简直日了*了. 现在用python在处理日志数据. 主要涉及 matplotlib,open和循环的使用. 日志内容大致如下 2016-10-21 21:07:59 ...

  8. ubuntu运行命令tee显示和保存为log

    一般有三种需求: 假如我要执行一个py文件 python class.py 1:将命令输出结果保存到文件log.log python class.py |tee log.log 结果就是:屏幕输出和直 ...

  9. ORACLE LOG的管理

    CREATE OR REPLACE PACKAGE PLOG IS /** * package name : PLOG *<br/> *<br/> *See : <a h ...

随机推荐

  1. JAVA笔记整理(四),JAVA中的封装

    什么是封装 所谓的封装就是把数据项和方法作为一个独立的整体隐藏在对象的内部,具体的实施细节不对外提现,仅仅保留有限的外部接口,封装外的用户只能通过接口来进行操作.就好比开启一台电脑需要进行很多个步骤, ...

  2. 极度舒适的 Python 入门教程,小猪佩奇也能学会~

    编程几乎已经成为现代人的一门必修课,特别是 Python ,不仅长期霸占编程趋势榜.薪资榜第一,还屡屡进入小学教材,甚至成为浙江省信息技术高考项目-- 今天,小编带来了一门极度舒适的 Python 入 ...

  3. HTML&CSS基础-相对定位

    HTML&CSS基础-相对定位 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.HTML源代码 <!DOCTYPE html> <html> &l ...

  4. tinymce + vue 富文本编辑

    用texterea最多支持换行,如果文本信息想要更加丰富一些,比如增加格式样式,比如增加图片,textarea就爱莫能助了 在网上搜寻了一番,发现tinymce是比较方便好用的一款富文本编辑 http ...

  5. Alpha版本发布

                Alpha版本发布   Part1.基本介绍 作业所属课程 课程链接 作业要求 要求链接 团队名称 Typhoon 作业目标 通过本次团队项目,体验通过使用软件工程来研发项目的 ...

  6. js基础知识2

    DOM Document Object Model 文档          对象       模型 对象: 属性和方法 属性:获取值和赋值 方法:赋值方法和条用方法 DOM树 document hea ...

  7. 【动态规划】ZZNU-OJ- 2054 : 油田

    2054 : 油田 (一个神奇的功能:点击上方文字进入相应页面) 时间限制:1 Sec 内存限制:32 MiB提交:49 答案正确:6 提交 状态 讨论区 题目描述 在太平洋的一片海域,发现了大量的油 ...

  8. 一次vaccum导致的事故

    1. 问题出现 晚上9点,现场报系统查询慢,运维查询zabbix后发现postgres最近几天的IOWait很大 2. 追踪问题 查询数据库,发现很多SQL堵住了 原因是真正创建index,导致表锁住 ...

  9. native关键字

    1.native关键字说明其修饰的方法是一个原生态方法,方法对应的实现不是在当前文件,而是在用其他语言(如C和C++)实现的文件中. 可以将native方法比作Java程序同C程序的接口

  10. SIGAI机器学习第三集 数学知识-2

    讲授机器学习相关的高等数学.线性代数.概率论知识 大纲: 最优化中的基本概念梯度下降法牛顿法坐标下降法数值优化算法面临的问题拉格朗日乘数法凸优化问题凸集凸函数凸优化拉格朗日对偶KKT条件 最优化中的基 ...