能否请您检阅我快速诠释解析器的实现? [英] Could you please review my Quick Int Parser implementation?

查看:132
本文介绍了能否请您检阅我快速诠释解析器的实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些,可以分析32位工具方法的整数比签署 Int32.Parse 快得多。我希望你可以通过查看我的代码,并建议改进或指出的错误给我一些你的经验。如果你有兴趣(并具有当然的时间),我将非常感激。

I wrote some utility methods that can parse a 32-bit signed integer much faster than Int32.Parse. I hope you could give me some of your experience by reviewing my code and suggesting enhancements or pointing out bugs. If you are interested (and have the time of course), I would be very grateful.

我已经发布我的代码修改我的密码网站上。这里是链接:
http://refactormycode.com/codes/699- INT32-快速解析器

I have posted my code on the "Refactor My Code" website. Here is the link: http://refactormycode.com/codes/699-int32-quick-parser

感谢您。


编辑:感谢大家对你有帮助的建议。我按照你的想法,这里是更新的版本:
HTTP: //refactormycode.com/codes/699-int32-quick-parser#refactor_142758

Thanks everybody for your helpful suggestions. I have followed your ideas, and here is the updated version: http://refactormycode.com/codes/699-int32-quick-parser#refactor_142758

(请使用diff工具才能看到更改后的代码行。)

(Please use a diff utility to see the changed code lines.)

感谢您!

推荐答案

我觉得你的实现是相当不错的。我做了使用指针运算,这确实是快,但在我看来脏不安全版本

I think your implementation is pretty decent. I've made an unsafe version using pointer math, which is indeed faster, but is dirtier in my opinion.

业绩(运行10M随机字符串INT 30个样品):

Results (running 30 samples of 10M random int strings):

Int32.Parse                      1766.17 ms (stddev: 1.83 ms)
Numbers.Int32.ParseTrusted       262.07 ms (stddev: 0.44 ms)
ParseTrustedUnsafe               146.13 ms (stddev: 0.43 ms)

这是我的不安全实现:

here is my unsafe implementation:

public static unsafe int ParseTrustedUnsafe(string str, int start, int end)
{
    unsafe
    {
    	Int32 result = 0;
    	Int32 length = end - start;
    	Boolean isNegative = false;
    	fixed (Char* startChar = str)
    	{
    		Byte* currentChar = ((Byte*)startChar) + (start * 2);
    		if (*currentChar == 0x2D)
    		{
    			isNegative = true;
    			currentChar += 2;
    			length--;
    		}
    		else if (*currentChar == 0x2B)
    		{
    			currentChar += 2;
    			length--;
    		}
    		while (length >= 4)
    		{
    			result = (result * 10) + (*currentChar - 0x30);
    			result = (result * 10) + (*(currentChar + 2) - 0x30);
    			result = (result * 10) + (*(currentChar + 4) - 0x30);
    			result = (result * 10) + (*(currentChar + 6) - 0x30);

    			currentChar += 8;
    			length -= 4;
    		}
    		while (length > 0)
    		{
    			result = (result * 10) + (*currentChar - 0x30);

    			currentChar += 2;
    			length -= 1;
    		}
    	}
    	return isNegative ? -result : result;
    }
}



下面是我的验证和基准测试程序:

Below is my validation and benchmark program:

static void Main(string[] args)
{
    String[] testValues = new String[10 * 100 * 100 * 100];
    Random rand = new Random();
    for (int i = 0; i < testValues.Length; i++)
    {
    	testValues[i] = rand.Next().ToString();
    }
    // validate
    for (int i = 0; i < testValues.Length; i++)
    {
    	Debug.Assert(String.CompareOrdinal(testValues[i], ParseTrustedUnsafe(testValues[i], 0, testValues[i].Length).ToString()) == 0, "ParseTrustedUnsafe failed on " + testValues[i]);
    	Debug.Assert(String.CompareOrdinal(testValues[i], Numbers.Int32.ParseTrusted(testValues[i], 0, testValues[i].Length).ToString()) == 0, "Numbers.Int32.ParseTrusted failed on " + testValues[i]);
    }
#if DEBUG
    return;
#endif    
    List<List<Double>> results = new List<List<double>>();
    for (int i = 0; i < 3; i++)
    {
    	results.Add(new List<double>());
    }

    Thread.CurrentThread.Priority = ThreadPriority.Highest;
    Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
    if (Environment.ProcessorCount > 1)
    {
    	Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(1 << (Environment.ProcessorCount - 1)); // use last core only
    }
    Stopwatch sw = new Stopwatch();
    for (int testrun = 0; testrun < 30; testrun++)
    {
    	sw.Reset();
    	sw.Start();
    	for (int i = 0; i < testValues.Length; i++)
    	{
    		Int32.Parse(testValues[i]);
    	}
    	sw.Stop();
    	results[0].Add(sw.ElapsedMilliseconds);

    	sw.Reset();
    	sw.Start();
    	for (int i = 0; i < testValues.Length; i++)
    	{
    		Numbers.Int32.ParseTrusted(testValues[i]);
    	}
    	sw.Stop();
    	results[1].Add(sw.ElapsedMilliseconds);

    	sw.Reset();
    	sw.Start();
    	for (int i = 0; i < testValues.Length; i++)
    	{
    		ParseTrustedUnsafe(testValues[i], 0, testValues[0].Length);
    	}
    	sw.Stop();
    	results[2].Add(sw.ElapsedMilliseconds);

    }
    Console.WriteLine("Int32.Parse \t\t\t {0:0.00}ms (stddev: {1:0.00}ms)", results[0].Average(), StandardDeviation(results[0]));
    Console.WriteLine("Numbers.Int32.ParseTrusted \t {0:0.00}ms (stddev: {1:0.00}ms)", results[1].Average(), StandardDeviation(results[1]));
    Console.WriteLine("ParseTrustedUnsafe \t\t {0:0.00}ms (stddev: {1:0.00}ms)", results[2].Average(), StandardDeviation(results[2]));
}

private static double StandardDeviation(IEnumerable<double> doubleList)
{
    double average = doubleList.Average();
    double sumOfDerivation = 0;
    foreach (double value in doubleList)
    {
    	sumOfDerivation += (value) * (value);
    }
    double sumOfDerivationAverage = sumOfDerivation / doubleList.Count();
    return Math.Sqrt(sumOfDerivationAverage - (average * average));
}

这篇关于能否请您检阅我快速诠释解析器的实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆