public void ToStringFast2() { for (int i = 0; i < N; i++) { FastToString.ToStringFast(data, 0, (uint)12); } }
public void ToStringSimple2() { for (int i = 0; i < N; i++) { FastToString.ToStringSimple(data, 0, 12); } }
public unsafe static void RunUIntToString() { char[] buffer = new char[1024]; int pos = 0; long sum = 0; uint v = uint.MaxValue - int.MaxValue; //byte v = 125; string s = v.ToString(); fixed(char *pd = &buffer[0]) { Stopwatch w = Stopwatch.StartNew(); for (int i = 0; i < 100000000; i++) { pos = 0; //FastToString.ToString2(pd, ref pos, v); //s = v.ToString(); //pos = FastToString.ToStringFast(pd, pos, v); pos = FastToString.ToString(pd, v); //sum += (int)(v / 10); } w.Stop(); if (new string(pd, 0, pos) != s) { Console.WriteLine("error"); } Console.WriteLine(w.ElapsedMilliseconds + " " + buffer + " " + sum); } }
public unsafe static void RunIntToStringValidity() { char[] buffer = new char[1024]; int pos = 0; fixed(char *pd = &buffer[0]) { Stopwatch w = Stopwatch.StartNew(); int n = 0; for (int i = int.MinValue; i < int.MaxValue; i++) { pos = 0; FastToString.ToString1(pd, ref pos, i); //if (new string(pd, 0, pos) != i.ToString()) // Console.WriteLine("error" + i.ToString()); n++; if (n % 100000000 == 0) { Console.WriteLine(n); } } w.Stop(); Console.WriteLine(w.ElapsedMilliseconds + " " + buffer); } }
public void TestMethod1() { char[] data = new char[20]; int n = FastToString.ToStringFast(data, 0, (uint)123456); string s = new string(data, 0, n); Assert.AreEqual(s, "123456"); }
public unsafe void ToStringFast1Point() { fixed(char *pd = &data[11]) { for (int i = 0; i < N; i++) { FastToString.ToStringFast(pd, 0, (uint)1); } } }
public unsafe void ToStringFast1PointByte() { fixed(byte *pd = &datab[0]) { for (int i = 0; i < N; i++) { FastToString.ToString(pd, (byte)1); } } }
public unsafe static void ConvertPrimitiveTypeClass(PrimitiveTypeClass[] list) { var varSize = 0; for (int i = 0; i < list.Length; i++) { var v = list[i]; varSize += v.V10.Length * 3 + 1; } var baseSize = 52; var headerSize = 36; var header = Encoding.UTF8.GetBytes("V0,V1,V2,V3,V4,V5,V6,V7,V10,V12,V13"); var len = 0; var bytes = ArrayPool <byte> .Shared.Rent(list.Length *(baseSize + 1) + headerSize * 3); var buffer = new Span <byte>(bytes); header.CopyTo(buffer); for (int i = 0; i < list.Length; i++) { var v = list[i]; fixed(byte *p = buffer) { var ptr = p; len = FastToString.ToStringNumber(ptr, v.V0); ptr += len; //len = FastToString.ToStringNumber(ptr, v.V1); //ptr += len; //len = FastToString.ToStringNumber(ptr, v.V2); //ptr += len; //len = FastToString.ToStringNumber(ptr, v.V3); //ptr += len; //len = FastToString.ToStringNumber(ptr, v.V4); //ptr += len; //len = FastToString.ToStringNumber(ptr, v.V5); //ptr += len; //len = FastToString.ToStringNumber(ptr, v.V6); //ptr += len; //len = FastToString.ToStringNumber(ptr, v.V7); //ptr += len; //len = FastToString.ToStringNumber(ptr, v.V10); //ptr += len; //len = FastToString.ToStringNumber(ptr, v.V12); //ptr += len; //len = FastToString.ToStringNumber(ptr, v.V13); //ptr += len; } } }
internal unsafe void Write(Uri value) { Resize((value.AbsoluteUri.Length) + 10); fixed(char *pd = &_buffer[position]) { char *p = pd; p = FastToString.ToString(p, ref position, value); *p++ = '\r'; *p++ = '\n'; position += 2; } }
internal unsafe void Write(Guid value) { fixed(char *pd = &_buffer[position]) { char *p = pd; *p++ = Quote; p = FastToString.ToString(p, ref position, value); *p++ = Quote; *p++ = Separator; position += 3; } }
internal unsafe void Write(int[] value) { fixed(char *pd = &_buffer[position]) { char *p = pd; for (int i = 0; i < value.Length - 1; i++) { p = FastToString.ToString(p, ref position, value[i]); *p++ = ','; position++; } p = FastToString.ToString(p, ref position, value[value.Length - 1]); *p++ = ']'; *p = ','; position += 2; } }
/// <summary> /// 仅供参考对照 /// </summary> /// <param name="list"></param> /// <param name="buffer"></param> /// <returns></returns> public static unsafe int ToCsv(Int8Class[] list, out Span <byte> buffer) { var bytes = ArrayPool <byte> .Shared.Rent(list.Length * 50); buffer = new Span <byte>(bytes); for (int i = 0; i < list.Length; i++) { var v = list[i]; fixed(byte *p = buffer) { var ptr = p; var len = 0; len = FastToString.ToStringNumber(ptr, v.V0); ptr += len; len = FastToString.ToStringNumber(ptr, v.V1); ptr += len; len = FastToString.ToStringNumber(ptr, v.V2); ptr += len; len = FastToString.ToStringNumber(ptr, v.V3); ptr += len; len = FastToString.ToStringNumber(ptr, v.V4); ptr += len; len = FastToString.ToStringNumber(ptr, v.V5); ptr += len; len = FastToString.ToStringNumber(ptr, v.V6); ptr += len; len = FastToString.ToStringNumber(ptr, v.V7); ptr += len; len = FastToString.ToStringNumber(ptr, v.V8); ptr += len; len = FastToString.ToStringNumber(ptr, v.V9); ptr += len; } } ArrayPool <byte> .Shared.Return(bytes); return(0); }
public static void GuidWriter() { Guid guid = Guid.NewGuid(); Console.WriteLine(guid); char[] buffer = new char[50]; Stopwatch w = Stopwatch.StartNew(); for (int j = 0; j < 10000000; j++) { //v = int.Parse(s); //v = FastToValue.ToInt32(s, ref pos); FastToString.WriteGuid(guid, buffer); //FastToString.WriteGuid(buffer,0,guid); //FastToString.ToString(buffer, 0, guid); //v = FastToValue.ToInt32For(s); //guid.ToString(); } w.Stop(); Console.WriteLine(w.ElapsedMilliseconds + "-" + guid); Console.WriteLine(new string(buffer, 0, 36)); }
public unsafe static void Int32Writer() { int v = -534011720; char[] buffer = new char[32]; int count = 6; Stopwatch w = Stopwatch.StartNew(); for (int j = 0; j < 1000000; j++) { //v.ToString(); //v = int.Parse(s); //v = FastToValue.ToInt32(s, ref pos); //FastToValue.CustomWriteInt(v, buffer); //FastToString.ToStringSimple(buffer, 0, v); count += FastToString.ToStringFast(buffer, count, v); //FastToString.ToStringFast(buffer,ref count, v); //fixed (char* pd = &buffer[count]) //{ // count += FastToString.ToStringFast(pd, count, v); //} //FastToString.CustomWriteUInt(buffer, 0, v); //FastToString.ToString(buffer, 0, v); //v = FastToValue.ToInt32For(s); //string s = FastToString.ToString(v); //Console.WriteLine(s); count = 6; } w.Stop(); Console.WriteLine(buffer); Console.WriteLine(new string(buffer, 0, count)); Console.WriteLine(w.ElapsedMilliseconds + " " + v); }
internal void Write(int value) { position += FastToString.ToString(_buffer, position, value); this._buffer[position] = Separator; position++; }
/// <summary> /// 234 三个长度测试 /// 测试A:407ms /// 测试B:273ms /// </summary> public unsafe static void Run() { TestDig(); return; var buffer = stackalloc byte[100]; var value = (byte)234; var count = 100000000; var length = 0; var w = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { length = FastToString.ToString(buffer, value); } Console.WriteLine($"A first cost:{w.ElapsedMilliseconds}"); w = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { length = FastToString.ToStringNumber(buffer, value); } Console.WriteLine($"B first cost:{w.ElapsedMilliseconds}"); w = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { length = NumberHelper.ToString(buffer, value); } Console.WriteLine($"C first cost:{w.ElapsedMilliseconds}"); w = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { length = value.ToString().Length; } Console.WriteLine($"D first cost:{w.ElapsedMilliseconds}"); w = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { length = FastToString.ToString(buffer, value); } Console.WriteLine($"A second cost:{w.ElapsedMilliseconds}"); w = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { length = FastToString.ToStringNumber(buffer, value); } Console.WriteLine($"B second cost:{w.ElapsedMilliseconds}"); w = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { length = NumberHelper.ToString(buffer, value); } Console.WriteLine($"C second cost:{w.ElapsedMilliseconds}"); w = Stopwatch.StartNew(); for (int i = 0; i < count; i++) { length = value.ToString().Length; } Console.WriteLine($"D second cost:{w.ElapsedMilliseconds}"); Console.WriteLine(*(buffer + 0)); Console.WriteLine(*(buffer + 1)); Console.WriteLine(*(buffer + 2)); //Console.WriteLine(value.ToString() == new string(buffer, 0, length)); }
public void Write(decimal value) { position += FastToString.ToString(_buffer, position, value); this._buffer[position] = Separator; position++; }
public unsafe static ArraySegment <byte> ConvertInt8Class(Int8Class[] list) { var varSize = 0; for (int i = 0; i < list.Length; i++) { var v = list[i]; } var baseSize = 40; var headerSize = 30; var len = 0; var bytes = ArrayPool <byte> .Shared.Rent(list.Length *(baseSize + 1) + headerSize); var buffer = new Span <byte>(bytes); header.CopyTo(buffer); fixed(byte *pd = &buffer[header.Length]) { var ptr = pd; for (int i = 0; i < list.Length; i++) { var v = list[i]; len = FastToString.ToStringNumber(ptr, v.V0); ptr += len; *ptr = (byte)','; ptr++; len = FastToString.ToStringNumber(ptr, v.V1); ptr += len; *ptr = (byte)','; ptr++; len = FastToString.ToStringNumber(ptr, v.V2); ptr += len; *ptr = (byte)','; ptr++; len = FastToString.ToStringNumber(ptr, v.V3); ptr += len; *ptr = (byte)','; ptr++; len = FastToString.ToStringNumber(ptr, v.V4); ptr += len; *ptr = (byte)','; ptr++; len = FastToString.ToStringNumber(ptr, v.V5); ptr += len; *ptr = (byte)','; ptr++; len = FastToString.ToStringNumber(ptr, v.V6); ptr += len; *ptr = (byte)','; ptr++; len = FastToString.ToStringNumber(ptr, v.V7); ptr += len; *ptr = (byte)','; ptr++; len = FastToString.ToStringNumber(ptr, v.V8); ptr += len; *ptr = (byte)','; ptr++; len = FastToString.ToStringNumber(ptr, v.V9); ptr += len; *ptr = (byte)'\n'; ptr++; } var size = header.Length + (int)(ptr - pd); return(new ArraySegment <byte>(bytes, 0, size)); } }
internal unsafe void Write(byte value) { //position += FastToString.ToString(_buffer, position, value); //this._buffer[position] = Separator; //position++; //int value = v; fixed(char *ptr = &_buffer[position]) { FastToString.ToStringSign(ptr, value, ref position); //char* buffer = ptr; //if (value < 10) //{ // *buffer = (char)(value + (char)'0'); // *(buffer + 1) = Separator; // position += 2; //} //else if (value < 100) //{ // var tens = (char)((value * 205u) >> 11); // div10, valid to 1028 // *buffer = (char)(tens + (char)'0'); // *(buffer + 1) = (char)(value - (tens * 10) + (char)'0'); // *(buffer + 2) = Separator; // position += 3; //} //else //{ // var digit0 = (char)((value * 41u) >> 12); // div100, valid to 1098 // var digits01 = (char)((value * 205u) >> 11); // div10, valid to 1028 // *buffer = (char)(digit0 + (char)'0'); // *(buffer + 1) = (char)(digits01 - (digit0 * 10) + (char)'0'); // *(buffer + 2) = (char)(value - (digits01 * 10) + (char)'0'); // *(buffer + 3) = Separator; // position += 4; //} } //if (value < 10) //{ // *bp = (char)(value + (char)'0'); // *(bp + 1) = Separator; // bp += 2; // position += 2; //} //else if (value < 100) //{ // var tens = (char)((value * 205u) >> 11); // div10, valid to 1028 // *bp = (char)(tens + (char)'0'); // *(bp + 1) = (char)(value - (tens * 10) + (char)'0'); // *(bp + 2) = Separator; // bp += 3; // position += 3; //} //else //{ // var digit0 = (char)((value * 41u) >> 12); // div100, valid to 1098 // var digits01 = (char)((value * 205u) >> 11); // div10, valid to 1028 // *bp = (char)(digit0 + (char)'0'); // *(bp + 1) = (char)(digits01 - (digit0 * 10) + (char)'0'); // *(bp + 2) = (char)(value - (digits01 * 10) + (char)'0'); // *(bp + 3) = Separator; // bp += 4; // position += 4; //} }