- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我从 Stack Overflow 问题中获取了代码示例 Disruptor.NET example 并将其修改为“测量”时间。完整列表如下:
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Disruptor;
using Disruptor.Dsl;
namespace DisruptorTest
{
public sealed class ValueEntry
{
public long Value { get; set; }
public ValueEntry()
{
Console.WriteLine("New ValueEntry created");
}
}
public class ValueAdditionHandler : IEventHandler<ValueEntry>
{
public void OnNext(ValueEntry data, long sequence, bool endOfBatch)
{
Program.sw.Stop();
long microseconds = Program.sw.ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
Console.WriteLine("elapsed microseconds = " + microseconds);
Console.WriteLine("Event handled: Value = {0} (processed event {1}", data.Value, sequence);
}
}
class Program
{
public static Stopwatch sw = Stopwatch.StartNew();
private static readonly Random _random = new Random();
private static readonly int _ringSize = 16; // Must be multiple of 2
static void Main(string[] args)
{
var disruptor = new Disruptor.Dsl.Disruptor<ValueEntry>(() => new ValueEntry(), _ringSize, TaskScheduler.Default);
disruptor.HandleEventsWith(new ValueAdditionHandler());
var ringBuffer = disruptor.Start();
while (true)
{
var valueToSet = _random.Next();
long sequenceNo = ringBuffer.Next();
ValueEntry entry = ringBuffer[sequenceNo];
entry.Value = valueToSet;
sw.Restart();
ringBuffer.Publish(sequenceNo);
Console.WriteLine("Published entry {0}, value {1}", sequenceNo, entry.Value);
Thread.Sleep(1000);
}
}
}
}
输出是:
New ValueEntry created
New ValueEntry created
New ValueEntry created
New ValueEntry created
New ValueEntry created
New ValueEntry created
New ValueEntry created
New ValueEntry created
New ValueEntry created
New ValueEntry created
New ValueEntry created
New ValueEntry created
New ValueEntry created
New ValueEntry created
New ValueEntry created
New ValueEntry created
Published entry 0, value 1510145842
elapsed microseconds = 2205
Event handled: Value = 1510145842 (processed event 0
Published entry 1, value 1718075893
elapsed microseconds = 85
Event handled: Value = 1718075893 (processed event 1
Published entry 2, value 1675907645
elapsed microseconds = 32
Event handled: Value = 1675907645 (processed event 2
Published entry 3, value 1563009446
elapsed microseconds = 75
Event handled: Value = 1563009446 (processed event 3
Published entry 4, value 1782914062
elapsed microseconds = 34
Event handled: Value = 1782914062 (processed event 4
Published entry 5, value 1516398244
elapsed microseconds = 50
Event handled: Value = 1516398244 (processed event 5
Published entry 6, value 76829327
elapsed microseconds = 50
Event handled: Value = 76829327 (processed event 6
因此将数据从一个线程传递到另一个线程大约需要 50 微秒。但是一点都不快! “当前版本的 Disruptor 可以以每秒 100 万条消息的速度在线程之间执行约 50 ns。”所以我的结果比预期慢 1000 倍。
我的示例有什么问题以及如何实现 50 纳秒的速度?
我修改了上面的程序,现在收到 1 微秒的延迟,这要好得多。但是,我仍在等待 disruptor
模式专家的回应。我正在寻找一个可以证明我实际上可以在 50 纳秒内传递数据的示例。
我还使用 BlockingCollection
编写了相同的测试,平均收到 14 微秒,这证明 Disruptor
更快:
使用 BlockingCollection:
average = 14 minimum = 0 0-5 = 890558, 5-10 = 1773781, 10-30 = 6900128, >30 = 435433
使用干扰器:
average = 0 minimum = 0 0-5 = 9908469, 5-10 = 64464, 10-30 = 19902, >30 = 7065
BlockingCollection 代码:
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace DisruptorTest
{
public sealed class ValueEntry
{
public int Value { get; set; }
public ValueEntry()
{
// Console.WriteLine("New ValueEntry created");
}
}
//public class ValueAdditionHandler : IEventHandler<ValueEntry>
//{
// public void OnNext(ValueEntry data, long sequence, bool endOfBatch)
// {
// long microseconds = Program.sw[data.Value].ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
// Program.results[data.Value] = microseconds;
// //Console.WriteLine("elapsed microseconds = " + microseconds);
// //Console.WriteLine("Event handled: Value = {0} (processed event {1}", data.Value, sequence);
// }
//}
class Program
{
public const int length = 10000000;
public static Stopwatch[] sw = new Stopwatch[length];
public static long[] results = new long[length];
static BlockingCollection<ValueEntry> dataItems = new BlockingCollection<ValueEntry>(150);
static void Main(string[] args)
{
for (int i = 0; i < length; i++)
{
sw[i] = Stopwatch.StartNew();
}
// A simple blocking consumer with no cancellation.
Task.Factory.StartNew(() =>
{
while (!dataItems.IsCompleted)
{
ValueEntry ve = null;
try
{
ve = dataItems.Take();
long microseconds = sw[ve.Value].ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
results[ve.Value] = microseconds;
//Console.WriteLine("elapsed microseconds = " + microseconds);
//Console.WriteLine("Event handled: Value = {0} (processed event {1}", ve.Value, ve.Value);
}
catch (InvalidOperationException) { }
}
}, TaskCreationOptions.LongRunning);
for (int i = 0; i < length; i++)
{
var valueToSet = i;
ValueEntry entry = new ValueEntry();
entry.Value = valueToSet;
sw[i].Restart();
dataItems.Add(entry);
//Console.WriteLine("Published entry {0}, value {1}", valueToSet, entry.Value);
//Thread.Sleep(1000);
}
// Wait until all events are delivered
Thread.Sleep(5000);
long average = 0;
long minimum = 10000000000;
int firstFive = 0;
int fiveToTen = 0;
int tenToThirty = 0;
int moreThenThirty = 0;
// Do not count first 100 items because they could be extremely slow
for (int i = 100; i < length; i++)
{
average += results[i];
if (results[i] < minimum)
{
minimum = results[i];
}
if (results[i] < 5)
{
firstFive++;
}
else if (results[i] < 10)
{
fiveToTen++;
}
else if (results[i] < 30)
{
tenToThirty++;
} else
{
moreThenThirty++;
}
}
average /= (length - 100);
Console.WriteLine("average = {0} minimum = {1} 0-5 = {2}, 5-10 = {3}, 10-30 = {4}, >30 = {5}", average, minimum, firstFive, fiveToTen, tenToThirty, moreThenThirty);
}
}
}
干扰代码:
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Disruptor;
using Disruptor.Dsl;
namespace DisruptorTest
{
public sealed class ValueEntry
{
public int Value { get; set; }
public ValueEntry()
{
// Console.WriteLine("New ValueEntry created");
}
}
public class ValueAdditionHandler : IEventHandler<ValueEntry>
{
public void OnNext(ValueEntry data, long sequence, bool endOfBatch)
{
long microseconds = Program.sw[data.Value].ElapsedTicks / (Stopwatch.Frequency / (1000L * 1000L));
Program.results[data.Value] = microseconds;
//Console.WriteLine("elapsed microseconds = " + microseconds);
//Console.WriteLine("Event handled: Value = {0} (processed event {1}", data.Value, sequence);
}
}
class Program
{
public const int length = 10000000;
public static Stopwatch[] sw = new Stopwatch[length];
public static long[] results = new long[length];
private static readonly Random _random = new Random();
private static readonly int _ringSize = 1024; // Must be multiple of 2
static void Main(string[] args)
{
for (int i = 0; i < length; i++)
{
sw[i] = Stopwatch.StartNew();
}
var disruptor = new Disruptor.Dsl.Disruptor<ValueEntry>(() => new ValueEntry(), _ringSize, TaskScheduler.Default);
disruptor.HandleEventsWith(new ValueAdditionHandler());
var ringBuffer = disruptor.Start();
for (int i = 0; i < length; i++)
{
var valueToSet = i;
long sequenceNo = ringBuffer.Next();
ValueEntry entry = ringBuffer[sequenceNo];
entry.Value = valueToSet;
sw[i].Restart();
ringBuffer.Publish(sequenceNo);
//Console.WriteLine("Published entry {0}, value {1}", sequenceNo, entry.Value);
//Thread.Sleep(1000);
}
// wait until all events are delivered
Thread.Sleep(5000);
long average = 0;
long minimum = 10000000000;
int firstFive = 0;
int fiveToTen = 0;
int tenToThirty = 0;
int moreThenThirty = 0;
// Do not count first 100 items because they could be extremely slow
for (int i = 100; i < length; i++)
{
average += results[i];
if (results[i] < minimum)
{
minimum = results[i];
}
if (results[i] < 5)
{
firstFive++;
}
else if (results[i] < 10)
{
fiveToTen++;
}
else if (results[i] < 30)
{
tenToThirty++;
}
else
{
moreThenThirty++;
}
}
average /= (length - 100);
Console.WriteLine("average = {0} minimum = {1} 0-5 = {2}, 5-10 = {3}, 10-30 = {4}, >30 = {5}", average, minimum, firstFive, fiveToTen, tenToThirty, moreThenThirty);
}
}
}
最佳答案
在这里,我修复了你的代码:
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Disruptor;
using Disruptor.Dsl;
namespace DisruptorTest
{
public sealed class ValueEntry
{
public int Value { get; set; }
public ValueEntry()
{
// Console.WriteLine("New ValueEntry created");
}
}
class Program
{
public const int length = 1000000;
public static Stopwatch sw;
private static readonly Random _random = new Random();
private static readonly int _ringSize = 1024; // Must be multiple of 2
static void Main(string[] args)
{
sw = Stopwatch.StartNew();
var disruptor = new Disruptor.Dsl.Disruptor<ValueEntry>(() => new ValueEntry(), _ringSize, TaskScheduler.Default);
var ringBuffer = disruptor.Start();
for (int i = 0; i < length; i++)
{
var valueToSet = i;
long sequenceNo = ringBuffer.Next();
ValueEntry entry = ringBuffer[sequenceNo];
entry.Value = valueToSet;
ringBuffer.Publish(sequenceNo);
//Console.WriteLine("Published entry {0}, value {1}", sequenceNo, entry.Value);
//Thread.Sleep(1000);
}
var elapsed = sw.Elapsed.Miliseconds();
// wait until all events are delivered
Thread.Sleep(10000);
double average = /(double)length;
Console.WriteLine("average = " + average);
}
}
}
这应该可以正确测试每件商品需要多长时间。
关于c# - 为什么我的破坏者例子这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13334778/
谁能给我一个关于如何使用函数 crypt_r() 的例子吗? 在手册页中,不清楚返回的 char * 字符串是指向函数本身内部(在堆中)分配的内存块,还是仍然指向静态内存,如 crypt()? 最佳答
在 Spectre 中paper ,有一个利用越界数组访问的示例(第 1.2 节)。代码是 if (x < array1_size) y = array2[ array1[x] * 256 ];
这是 Grammar: difference between a top down and bottom up? 的后续问题 我从这个问题中了解到: 语法本身不是自上而下或自下而上的,而是解析器 有些
在java的构造函数中声明变量合法吗?示例。 Time(){ long timeMill = System.currentTimeMillis(); int secon
我一直在仔细研究 slick grid 的示例,并且想要 ping SO 社区并查询 Excel 电子表格编辑演示的示例?就存储而言,网格仅存储整数数据,并且网格将托管在 mvc3 razor 页面内
我很难将愚蠢的菜单置于我网站页面的中心。我知道我可以将外部 div 的宽度设置为 px 值,但我怎样才能让它以响应式网站为中心?这是页面: http://103.4.17.225/~america/i
我正在寻找可在 wordpress 上使用的主题。有时,页面会在调整大小的网络浏览器上正确加载,但在移动设备上却不能,即使尺寸相同,它也会加载某种错误(通常是错位)。例如,在此页面中 ( http:/
union { unsigned char raw[8]; struct { uint8_t gz_method; uint8_t flag;
我想使用 matchShapes() 函数在查询图像中查找对象。 假设我有一本书的模型图像,我想提取它的形状,然后尝试在另一幅图像中找到这本书(它的形状)。 我在谷歌上搜索了很多,但找不到任何关于如何
我正在寻找一个使用 inotify 的简单、简洁的示例gem 来检测目录的更改。 它缺少示例。 最佳答案 examples/watcher.rb 中有一个示例.该链接指向 aredridel 的 re
我一直在努力学习编程中的递归是什么,我需要有人来确认我是否已经完全理解它是什么。 我尝试考虑的方式是通过对象之间的碰撞检测。 假设我们有一个函数。当确定发生碰撞时调用该函数,并使用对象列表调用它以确定
我正在尝试学习如何在我正在处理的项目中使用 jBullet,我已经查看了源提供的演示,但我只是无法弄清楚这些演示如何显示对象。谁有好的资源可以指点我或提供一个在屏幕上显示一个或两个对象的基本示例? 在
我想在一个简单的 x,y 图表上绘制线条,以使用 JGraphT 在 JApplet 中显示。我找到的例子不是很有帮助。有人可以给我指出一些简单的 JGraphT 示例吗? 最佳答案 这里有一个例子,
在解决几何问题时,我遇到了一种称为滑动窗口算法的方法。 真的找不到任何关于它的学习 Material /细节。 算法是关于什么的? 最佳答案 我认为它更像是一种技术而不是一种算法。这是一种可用于各种算
我正在学习同步方法,以防止 Java 中的竞争条件和不良行为。我看到了以下示例,并被告知竞争条件非常微妙: public class Messages { private String messa
我有 100 万个 pdf,如何使用 hadoop 转换为文本并将其用于分析。目标是利用 hadoop 的强大功能将 pdf 数据提取为文本。 最佳答案 我已经在 Hadoop 上处理了一个 pdf
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我读到过,由于堆栈展开,从析构函数中抛出不是一个好主意。我不确定我是否完全理解。所以我尝试了下面的例子 struct foo { ~foo() { throw 1;
任何人都可以告诉我一个简单的(代码)示例来展示 response.encodeURL() 的用法吗?我所有的搜索(包括 google 和 stackoverflow)只提供了 encodeURL()
我受困于 haskell 类型。 {-# LANGUAGE OverloadedStrings #-} module Main ( main ) where import qualified
我是一名优秀的程序员,十分优秀!