const int InsertCount = 150000;
var startTime = DateTime.Now;
var ropeOfChars = new BigList<char>();
for (int i = 0; i < InsertCount; i++)
{
ropeOfChars.Insert(0, (char)('a' + (i % 10)));
}
Console.WriteLine("Rope<char> time: {0}", DateTime.Now - startTime);
startTime = DateTime.Now;
var stringBuilder = new StringBuilder();
for (int i = 0; i < InsertCount; i++)
{
stringBuilder.Insert(0, (char)('a' + (i % 10)));
}
Console.WriteLine("StringBuilder time: {0}", DateTime.Now - startTime);
I measured its performance and it performs pretty well in "start of string inserts":
const int InsertCount = 150000;
var startTime = DateTime.Now;
var ropeOfChars = new BigList<char>();
for (int i = 0; i < InsertCount; i++)
{
ropeOfChars.Insert(0, (char)('a' + (i % 10)));
}
Console.WriteLine("Rope<char> time: {0}", DateTime.Now - startTime);
startTime = DateTime.Now;
var stringBuilder = new StringBuilder();
for (int i = 0; i < InsertCount; i++)
{
stringBuilder.Insert(0, (char)('a' + (i % 10)));
}
Console.WriteLine("StringBuilder time: {0}", DateTime.Now - startTime);
Here is a public implementation of Ropes in C#, based on the immutable java implementation listed above. Note that you won't get the same polymorphism benefits as the java version because strings can't be inherited and CharSequence doesn't exist natively in C#.
发布评论
评论(5)
我不知道 Rope 实现(尽管可能有一个!),但是如果您只是在进行串联之后,StringBuilder 将完成这项工作。
I'm not aware of a Rope implementation (though there probably is one!), but if you're only after doing concatenation, StringBuilder will do the job.
就其价值而言,这是一个不可变的 Java 实现。您可能可以在不到一个小时的时间内将其转换为 C#。
For what its worth, here is an immutable Java implementation. You could probably convert it to C# in less than an hour.
Wintellect Power Collections(一个 C# 数据结构库)中的
BigList
类在某种程度上类似于绳索:http://docs.pushtechnology.com/docs/4.5 .7/dotnet/externalclient/html/class_wintellect_1_1_power_collections_1_1_big_list_3_01_t_01_4.html
我测量了它的性能,它在“字符串插入开始”中表现得很好:
结果:
但在“字符串插入中间”中表现不佳:
结果:
我不确定这是否是一个错误或低效的实现,但“
rope of chars
”预计比 C# 中的StringBuilder
更快。您可以从 NuGet 安装 Power Collections:
The
BigList<T>
class from Wintellect Power Collections (a C# data structure library) is somehow similar to rope:http://docs.pushtechnology.com/docs/4.5.7/dotnet/externalclient/html/class_wintellect_1_1_power_collections_1_1_big_list_3_01_t_01_4.html
I measured its performance and it performs pretty well in "start of string inserts":
Results:
But it performs not well in "middle of string inserts":
Results:
I am not sure if this is a bug or unefficient implementation, but "
rope of chars
" is expected to be faster thatStringBuilder
in C#.You can install Power Collections from NuGet:
我的同事在此处用 C# 编写了 Rope 实现。
也可以作为 NuGet 包 FlatlinerDOA.Rope 提供。
My colleague has written a Rope implementation in C# here.
Also available as NuGet package FlatlinerDOA.Rope.
这里是 C# 中 Ropes 的公共实现,基于上面列出的不可变 java 实现。请注意,您不会获得与 java 版本相同的多态性优势,因为字符串无法继承,并且 C# 中本身不存在 CharSequence。
Here is a public implementation of Ropes in C#, based on the immutable java implementation listed above. Note that you won't get the same polymorphism benefits as the java version because strings can't be inherited and CharSequence doesn't exist natively in C#.