От: | matumba | ||
Дата: | 10.06.13 14:33 | ||
Оценка: |
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testSplit
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("prepare all data...");
var rnd = new Random();
var testData = new List<byte[]>();
for (int iter = 0; iter < 2000; iter++) {
byte[] arr = new byte[rnd.Next(100000, 200000)];
for (int i = 0; i < arr.Length; i++)
arr[i] = (byte)rnd.Next(256);
testData.Add(arr);
}
Console.WriteLine("Test started!");
var watch = System.Diagnostics.Stopwatch.StartNew();
for (int i = 0; i < 2000; i++) {
var res = Split(testData[i], 10);
}
watch.Stop();
Console.WriteLine("Elapsed sec: "+ (double)watch.ElapsedMilliseconds / 1000);
Console.ReadKey();
}
static List<byte[]> Split(byte[] arr, byte divisor)
{
int len;
var res = new List<byte[]>();
int SegmentStart = -1;
for (int i = 0; i < arr.Length; i++) {
if (arr[i] == divisor) {
len = i - SegmentStart - 1;
if (len == 0)
res.Add(null);
else{
var seg = new byte[len];
Array.Copy(arr, SegmentStart+1, seg, 0, len);
res.Add(seg);
}
SegmentStart = i;
}
}
len = arr.Length - SegmentStart - 1;
if (len > 0) {
var seg = new byte[len];
Array.Copy(arr, SegmentStart + 1, seg, 0, len);
res.Add(seg);
}
return res;
}
}
}
prepare all data...
Test started!
Elapsed sec: 0.33