C# 高性能的数组 高性能数组队列实战 HslCommunication的SharpList类详解
本文将使用一个gitHub开源的组件技术来实现这个功能
github地址:https://github.com/dathlin/HslCommunication
如果喜欢可以star或是fork,还可以打赏支持。
官网地址:http://www.hslcommunication.cn/ 打赏请认准官网

场景需求
我们会有对缓存数据的需求。C#本身提供了固定长度的数组 T[] , 可变长度的List<T> 当然还有先入先出,后入后出的队列。
通常实际中,我们需要维护一个缓存的数组队列,比如一个int数组,长度为1000个,当我们读取到数据后,需要往里面添加数据,然后所有的数据都是往左挪动。最后这个数据是线程安全的操作。
第一种写法,就是循环挪动数据:
int[] buffer = new int[1000];
hybirdLock.Enter( );
for (int j = 0; j < buffer.Length - 1; j++)
{
buffer[j] = buffer[j + 1];
}
buffer[999] = 100;
hybirdLock.Leave( );
第二种写法,批量挪动数据
int[] buffer = new int[1000];
hybirdLock.Enter( );
int[] newbuffer = new int[1000];
Array.Copy( buffer, 0, newbuffer, 0, 999 );
newbuffer[999] = 100;
buffer = newbuffer;
hybirdLock.Leave( );
第三种写法,就是List泛型类,和先入先出的用法差不多
int[] buffer = new int[1000];
List<int> list = new List<int>( buffer ); hybirdLock.Enter( ); list.Add( 100 );
list.RemoveAt( 0 ); hybirdLock.Leave( );
第四种写法:SharpList<T> 类型实现
SharpList<int> sharpList = new SharpList<int>( 1000, true );
sharpList.Add( 1000 );
初步对比,SharpList代码上更加精简。因为内置了线程安全,自动挪动数据。
上述代码我们定义了一个长度为1000的int类型的数组对象。实例化之后,其本身就是一个1000个长度的 int[] 数组,当 Add(100);时,最右侧就多了一个100的数据。
SharpList<T> 提供了几个方法来方便快捷的操作数据。比如根据索引为访问:
int value = sharpList[0]; // 得到0
int value2 = sharpList[999]; // 得到100 int[] tmp = sharpList.ToArray(); // 得到数组数据的副本。[0,,,,,,,,,,999]
当新增数据的时候,也支持批量的新增。
性能对比
我们将上述的四种方式各自运行100W次,查看下各自的性能差异,具体运行时间取决于cpu型号,内存,等因数,此处仅仅是一个参考。
SimpleHybirdLock hybirdLock = new SimpleHybirdLock( );
int[] buffer = new int[1000];
DateTime start = DateTime.Now;
for (int i = 0; i < 1000000; i++)
{
hybirdLock.Enter( );
for (int j = 0; j < buffer.Length - 1; j++)
{
buffer[j] = buffer[j + 1];
}
buffer[999] = i;
hybirdLock.Leave( );
}
Console.WriteLine( (DateTime.Now - start).TotalMilliseconds );
start = DateTime.Now;
for (int i = 0; i < 1000000; i++)
{
hybirdLock.Enter( );
int[] newbuffer = new int[1000];
Array.Copy( buffer, 0, newbuffer, 0, 999 );
newbuffer[999] = i;
buffer = newbuffer;
hybirdLock.Leave( );
}
Console.WriteLine( (DateTime.Now - start).TotalMilliseconds );
List<int> list = new List<int>( buffer );
start = DateTime.Now;
for (int i = 0; i < 1000000; i++)
{
hybirdLock.Enter( );
list.Add( i );
list.RemoveAt( 0 );
hybirdLock.Leave( );
}
Console.WriteLine( (DateTime.Now - start).TotalMilliseconds );
SharpList<int> sharpList = new SharpList<int>( 1000, true );
start = DateTime.Now;
for (int i = 0; i < 1000000; i++)
{
sharpList.Add( i );
}
Console.WriteLine( (DateTime.Now - start).TotalMilliseconds );
int[] data = sharpList.ToArray( );
Console.ReadLine( );
我们跑三次,对比结果。



我们看到SharpList<T> 类仅仅消耗了37ms,完成了100W次数据的新增和挪动。
为什么会有那么大的性能差异呢?就要深入源代码查看了。
深度剖析
超高的性能的本质在于减少大块的数据移动,先内部实例化一个远比需求还大的多的数据对象
array = new T[capacity + count];
当有数据新增进来的时候,实际不需要移动
/// <summary>
/// 新增一个数据值
/// </summary>
/// <param name="value">数据值</param>
public void Add( T value )
{
hybirdLock.Enter( ); if(lastIndex < (capacity + count))
{
array[lastIndex++] = value;
}
else
{
// 需要重新挪位置了
T[] buffer = new T[capacity + count];
Array.Copy( array, capacity, buffer, 0, count );
array = buffer;
lastIndex = count;
} hybirdLock.Leave( );
}
先进行自然的赋值,这时的性能就非常快了,然后提高游标的索引。当缓存都不够时,再去复制挪动一次数据。
当然,当要获取数据时,就需要进行根据当前的活动游标进行获取到正确的数据。
/// <summary>
/// 获取数据的数组值
/// </summary>
/// <returns>数组值</returns>
public T[] ToArray( )
{
T[] result = null;
hybirdLock.Enter( ); if (lastIndex < count)
{
result = new T[lastIndex];
Array.Copy( array, 0, result, 0, lastIndex );
}
else
{
result = new T[count];
Array.Copy( array, lastIndex - count, result, 0, count );
}
hybirdLock.Leave( );
return result;
}
相关的话题,后续补充。
C# 高性能的数组 高性能数组队列实战 HslCommunication的SharpList类详解的更多相关文章
- 最佳实战Docker持续集成图文详解
最佳实战Docker持续集成图文详解 这是一种真正的容器级的实现,这个带来的好处,不仅仅是效率的提升,更是一种变革:开发人员第一次真正为自己的代码负责——终于可以跳过运维和测试部门,自主维护运行环境( ...
- shell编程系列23--shell操作数据库实战之mysql命令参数详解
shell编程系列23--shell操作数据库实战之mysql命令参数详解 mysql命令参数详解 -u 用户名 -p 用户密码 -h 服务器ip地址 -D 连接的数据库 -N 不输出列信息 -B 使 ...
- Redis实战 | 5种Redis数据类型详解
我们知道Redis是目前非常主流的KV数据库,它因高性能的读写能力而著称,其实还有另外一个优势,就是Redis提供了更加丰富的数据类型,这使得Redis有着更加广泛的使用场景.那Redis提供给用户的 ...
- PHP数组的交集array_intersect(),array_intersect_assoc(),array_inter_key()函数详解
求两个数组的交集问题可以使用 array_intersect(),array_inersect_assoc,array_intersect_key来实现,其中 array_intersect()函数是 ...
- 遍历查找集合或者数组中的某个元素的值 java代码 详解 Android开发
import java.util.Scanner; public class Test21 { public static void main(String[] args) { //定义并初始化数组 ...
- Kafka高性能揭秘:sequence IO、PageCache、SendFile的应用详解
大家都知道Kafka是将数据存储于磁盘的,而磁盘读写性能往往很差,但Kafka官方测试其数据读写速率能达到600M/s,那么为什么Kafka性能会这么高呢? 首先producer往broker发送消息 ...
- Hadoop实战之二~ hadoop作业调度详解(1)
对Hadoop的最感兴趣的地方,也就在于Hadoop的作业调度了,在正式介绍如何搭建Hadoop之前,深入理解一下Hadoop的作业调度很有必要.我们不一定能用得上Hadoop,但是如果理通顺Hado ...
- reactjs入门到实战(五)---- props详解
1>>>基础的props使用 不可修改父属性 getDefaultProps 对于外界/父组件的属性值,无法直接修改,它是只读的. <script type= ...
- reactjs入门到实战(四)---- state详解
this.props 表示那些一旦定义,就不再改变的特性,而 this.state 是会随着用户互动而产生变化的特性. 组件免不了要与用户互动,React 的一大创新,就是将组件看成是一个状态机,一开 ...
随机推荐
- 理解Fragment的生命周期
与活动类似,Fragment也有自己的生命周期.理解Fragment的生命周期有助于在Fragment销毁时能恰当地保存其实例,然后在重新创建时能够将其恢复至之前的状态. 下面的“试一试”将研究Fra ...
- HDU 6090 Rikka with Graph
Rikka with Graph 思路: 官方题解: 代码: #include<bits/stdc++.h> using namespace std; #define ll long lo ...
- Google chrome浏览器打不开网页,显示ERR_Failed...等问题的解决方法
新装好的win7系统,打开Google浏览器,显示网页可能暂时无法连接,或者它已永久性的移动到了新地址.在网络搜索很多资料,发现解决方法如下,亲测成功. 原因,该服务依赖的TCP/IP 协议有问题. ...
- jsp动作之 forward
forward说明了,就想当于php的include,require函数.(但是它是跳转.forward之前的数据都不会显示) 这么说你明白了吗.就是包含,说的好听点就是跳转,但是url地址栏却是没有 ...
- DataGridView1
for (int i = 0; i < DataGridView1.SelectedRows.Count; i++) { //第i行第一列 String ai1= DataGridView ...
- [.NET开发] C#实现剪切板功能
C#剪切板 Clipboard类 我们现在先来看一下官方文档的介绍 位于:System.Windows.Forms 命名空间下 Provides methods to place data on an ...
- thinkphp数组处理
1.array_unique() 移除数组中的重复的值,并返回结果数组.当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除,对每个值只保留第一个遇到的键名,接着忽略所有后面的键名.返回的数组 ...
- nyoj306 二分+DFS
走迷宫 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 Dr.Kong设计的机器人卡多非常爱玩,它常常偷偷跑出实验室,在某个游乐场玩之不疲.这天卡多又跑出来了,在SJ ...
- POJ-3009 Curling 2.0 (DFS)
Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But th ...
- spring boot 学习番外篇:超快速项目初始化
超快速完成 Spring Boot 项目初始化 最近,在浏览 SPRING 官网时,发现一个超级方便的小工具,可以帮助我们快速创建一个 Spring Boot 项目,前提就是你能连接互联网. 依赖 支 ...