理解Python中的RingBuffer环形缓冲区

理解Python中的RingBuffer环形缓冲区ringbufferReferedfromWikipedia,aringbuffer(环形缓冲区orcircularbuffer,circularqueue,cyclicbuffer)isadatastrcturethatusesasingle,fixed-sizebufferasifitwereconnectedend-to-end.Thisstructurelendsitselfeasilytobufferingdatas..

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

  • ringbuffer

    Refered from Wikipedia, a ring buffer(环形缓冲区 or circular buffer, circular queue, cyclic buffer) is a data strcture that uses a single, fixed-size buffer as if it were connected end-to-end.

    This structure lends itself easily to buffering data streams.

  • buffer

    First, we should know what the “BUFFER” is in computer science.

    According Wikipedia, a data buffer (or just buffer) is a region of a physical memory storage used to temporarily store data while it is being moved from one place to another.

    Typically, the data is stored in a buffer as it is retrieved from an input device or just befor it is sent to an output device.

    However, a buffer may be used when moving data between processes within a computer.

  • LMAX Disruptor 中 ringbuffer

    LMAX是伦敦多元资产交易所的简称,https://www.lmax.com/

    其开源的LMAX Disruptor可以处理数百万订单/秒。ringbuffer是其一大利器。

    ringbuffer用于在不同上下文(线程)间传递数据的buffer。

    ringbuffer是数据(数组内元素的内存地址是连续性存储的)比链表快。

  • Python中实现ringbuffer

    网上相关资料不多。

    1. 自己实现

      refer from

      class RingBuffer:
          def __init__(self, size):
              self.data = [None for i in range(size)]
          
          def append(self, x):
              self.data.pop(0) # remove the last one which index = -1
              self.data.append(x) # add at the index = -1
          
          def get(self):
              return self.data
      
      buf = RingBuffer(4)
      for i in range(10):
          buf.append(i)
          print(buf.get())
      

      Python Cookbook by Alex Martelli, David Ascher 中有一种相对完善的实现:

      class RingBuffer:
          """ class that implements a not-yet-full buffer """
          def __init__(self,size_max):
              self.max = size_max
              self.data = []
      
          class __Full:
              """ class that implements a full buffer """
              def append(self, x):
                  """ Append an element overwriting the oldest one. """
                  self.data[self.cur] = x
                  self.cur = (self.cur+1) % self.max
              def get(self):
                  """ return list of elements in correct order """
                  return self.data[self.cur:]+self.data[:self.cur]
      
          def append(self,x):
              """append an element at the end of the buffer"""
              self.data.append(x)
              if len(self.data) == self.max:
                  self.cur = 0
                  # Permanently change self's class from non-full to full
                  self._ _class_ _ = self._ _Full
      
          def get(self):
              """ Return a list of elements from the oldest to the newest. """
              return self.data
      
      # sample usage
      if _ _name_ _=='_ _main_ _':
          x=RingBuffer(5)
          x.append(1); x.append(2); x.append(3); x.append(4)
          print x._ _class_ _, x.get(  )
          x.append(5)
          print x._ _class_ _, x.get(  )
          x.append(6)
          print x.data, x.get(  )
          x.append(7); x.append(8); x.append(9); x.append(10)
          print x.data, x.get(  )
      
    2. Python模块collections.deque

      class collections.deque([iterable[, maxlen]])

      deque对象返回一个新的双向队列对象。

    3. Python模块pyringbuf

      这个模块在2015年之后就没有更新了

  • References

  1. 剖析Disruptor:为什么会这么快?(一)Ringbuffer的特别之处
  2. 并发框架Disruptor译文
  3. disruptor – BlogsAndArticles.wiki
  4. Trisha’s Ramblings
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/197760.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • MFC进度条-转

    MFC进度条-转CProgressCtrl 控件属性当我们在处理大程序时,常常需要耗很长时间(比如搜索内存,复制大文件),为了不让用户错误的认为系统已经死机,我们要给程序添加进度条。CProgressC

    2022年6月30日
    25
  • restsharp 并发请求_JAVA应用

    restsharp 并发请求_JAVA应用C#RestSharp应用开通博客是想将自己所学的东西记录下来,以便自己查缺补漏,希望自己能坚持下去正题关于RestSharp的使用下载NuGet直接搜索即可,最新版本RestSharp需要.netframework4.5.2及以上支持Json序列化工具:Newtonsoft.Json,直接由NuGet下载官网说明:varclient=newRestClient(“http://ex…

    2022年9月8日
    0
  • 简述handler机制及其作用_传统金融的优势与缺点

    简述handler机制及其作用_传统金融的优势与缺点From:http://www.mysqlops.com/2011/10/20/handlersocket-adv.htmlHandlerSocket的优势和特点:1)        支持多种查询模式HandlerSocket目前支持索引查询(主键索引和非主键的普通索引均可),索引范围扫描,LIMIT子句,也即支持增加、删除、修改、查询完整功能,但还不支持无法使用任何索引

    2022年8月24日
    4
  • Redis 的 8 大数据类型,写得非常好!

    Redis 的 8 大数据类型,写得非常好!

    2022年2月17日
    30
  • IE中输入框绑定input事件触发解析(input事件初始化自动执行问题解决)

    IE中输入框绑定input事件触发解析(input事件初始化自动执行问题解决)在React项目中碰到了个问题,输入框绑定了input事件,在IE中初始化会自动执行,刚开始以为是只要有placeholder就会导致问题,后面网上搜了一轮,自己手撸了一下,总结了导致绑定的input事件自动执行的原因1.输入框的值为空,2.placeholder改变(注意这个改变的就算前后的placeholder值一样也算改变;还有就是input获得焦点时和失去焦点时,也会改变;但是改变前后…

    2022年6月1日
    73
  • bytebuffer是什么_byte与bit

    bytebuffer是什么_byte与bit一.ByteBuffer   ByteBuffer是JDKNIO中提供的Java.nio.Buffer,在内存中预留指定大小的存储空间来存放临时数据,其他Buffer的子类有:CharBuffer、DoubleBuffer、FloatBuffer、IntBuffer、LongBuffer和ShortBuffer 1.Buffer   ByteBuffer继承…

    2022年9月2日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号