当前位置:  开发笔记 > 编程语言 > 正文

解析性能(如果,TryParse,Try-Catch)

如何解决《解析性能(如果,TryParse,Try-Catch)》经验,为你挑选了3个好方法。

我对处理解析文本信息的不同方法了解很多.例如,对于解析整数,可以期望什么样的性能.我想知道是否有人知道这方面的任何好的统计数据.我正在寻找一些经过测试的人的真实数字.

在哪些情况下,哪些提供最佳性能?

Parse(...)  // Crash if the case is extremely rare .0001%

If (SomethingIsValid) // Check the value before parsing
    Parse(...)

TryParse(...) // Using TryParse

try
{
    Parse(...)
}
catch
{
    // Catch any thrown exceptions
}

user7116.. 62

始终使用T.TryParse(string str,out T value).抛出异常是昂贵的,如果你能够先验地处理这种情况,应该避免.使用try-catch块来"保存"性能(因为您的无效数据速率很低)是滥用异常处理,但代价是可维护性和良好的编码实践.遵循合理的软件工程开发实践,编写测试用例,运行应用程序,然后进行基准测试和优化.

"我们应该忘记小的效率,大约97%的时间说:过早的优化是所有邪恶的根源.但我们不应该放弃我们在那个关键的3%的机会"-Donald Knuth

因此,您可以在碳信用额中任意分配try-catch 的性能更差,并且TryParse的性能更好.只有在我们运行我们的应用程序并确定我们有某种减速wrt字符串解析之后,我们才会考虑使用除TryParse之外的任何东西.

(编辑:因为看起来提问者希望时间数据有良好的建议,这里是要求的时间数据)

来自用户的10,000个输入(针对非信徒)的各种失败率的时间:

Failure Rate      Try-Catch          TryParse        Slowdown
  0%           00:00:00.0131758   00:00:00.0120421      0.1
 10%           00:00:00.1540251   00:00:00.0087699     16.6
 20%           00:00:00.2833266   00:00:00.0105229     25.9
 30%           00:00:00.4462866   00:00:00.0091487     47.8
 40%           00:00:00.6951060   00:00:00.0108980     62.8
 50%           00:00:00.7567745   00:00:00.0087065     85.9
 60%           00:00:00.7090449   00:00:00.0083365     84.1
 70%           00:00:00.8179365   00:00:00.0088809     91.1
 80%           00:00:00.9468898   00:00:00.0088562    105.9
 90%           00:00:01.0411393   00:00:00.0081040    127.5
100%           00:00:01.1488157   00:00:00.0078877    144.6


/// Rate of errors in user input
/// Total time taken
public static TimeSpan TimeTryCatch(double errorRate, int seed, int count)
{
    Stopwatch stopwatch = new Stopwatch();
    Random random = new Random(seed);
    string bad_prefix = @"X";

    stopwatch.Start();
    for(int ii = 0; ii < count; ++ii)
    {
        string input = random.Next().ToString();
        if (random.NextDouble() < errorRate)
        {
           input = bad_prefix + input;
        }

        int value = 0;
        try
        {
            value = Int32.Parse(input);
        }
        catch(FormatException)
        {
            value = -1; // we would do something here with a logger perhaps
        }
    }
    stopwatch.Stop();

    return stopwatch.Elapsed;
}

/// Rate of errors in user input
/// Total time taken
public static TimeSpan TimeTryParse(double errorRate, int seed, int count)
{
    Stopwatch stopwatch = new Stopwatch();
    Random random = new Random(seed);
    string bad_prefix = @"X";

    stopwatch.Start();
    for(int ii = 0; ii < count; ++ii)
    {
        string input = random.Next().ToString();
        if (random.NextDouble() < errorRate)
        {
           input = bad_prefix + input;
        }

        int value = 0;
        if (!Int32.TryParse(input, out value))
        {
            value = -1; // we would do something here with a logger perhaps
        }
    }
    stopwatch.Stop();

    return stopwatch.Elapsed;
}

public static void TimeStringParse()
{
    double errorRate = 0.1; // 10% of the time our users mess up
    int count = 10000; // 10000 entries by a user

    TimeSpan trycatch = TimeTryCatch(errorRate, 1, count);
    TimeSpan tryparse = TimeTryParse(errorRate, 1, count);

    Console.WriteLine("trycatch: {0}", trycatch);
    Console.WriteLine("tryparse: {0}", tryparse);
}

亚里士多德从来没有通过实验来弄脏手.惭愧,羞耻.你需要断言一些事情显然是真的.这是互联网的方式! (2认同)


Kev.. 8

虽然我没有亲自描述不同的方式,但这个章节有:

http://blogs.msdn.com/ianhu/archive/2005/12/19/505702.aspx



1> user7116..:

始终使用T.TryParse(string str,out T value).抛出异常是昂贵的,如果你能够先验地处理这种情况,应该避免.使用try-catch块来"保存"性能(因为您的无效数据速率很低)是滥用异常处理,但代价是可维护性和良好的编码实践.遵循合理的软件工程开发实践,编写测试用例,运行应用程序,然后进行基准测试和优化.

"我们应该忘记小的效率,大约97%的时间说:过早的优化是所有邪恶的根源.但我们不应该放弃我们在那个关键的3%的机会"-Donald Knuth

因此,您可以在碳信用额中任意分配try-catch 的性能更差,并且TryParse的性能更好.只有在我们运行我们的应用程序并确定我们有某种减速wrt字符串解析之后,我们才会考虑使用除TryParse之外的任何东西.

(编辑:因为看起来提问者希望时间数据有良好的建议,这里是要求的时间数据)

来自用户的10,000个输入(针对非信徒)的各种失败率的时间:

Failure Rate      Try-Catch          TryParse        Slowdown
  0%           00:00:00.0131758   00:00:00.0120421      0.1
 10%           00:00:00.1540251   00:00:00.0087699     16.6
 20%           00:00:00.2833266   00:00:00.0105229     25.9
 30%           00:00:00.4462866   00:00:00.0091487     47.8
 40%           00:00:00.6951060   00:00:00.0108980     62.8
 50%           00:00:00.7567745   00:00:00.0087065     85.9
 60%           00:00:00.7090449   00:00:00.0083365     84.1
 70%           00:00:00.8179365   00:00:00.0088809     91.1
 80%           00:00:00.9468898   00:00:00.0088562    105.9
 90%           00:00:01.0411393   00:00:00.0081040    127.5
100%           00:00:01.1488157   00:00:00.0078877    144.6


/// Rate of errors in user input
/// Total time taken
public static TimeSpan TimeTryCatch(double errorRate, int seed, int count)
{
    Stopwatch stopwatch = new Stopwatch();
    Random random = new Random(seed);
    string bad_prefix = @"X";

    stopwatch.Start();
    for(int ii = 0; ii < count; ++ii)
    {
        string input = random.Next().ToString();
        if (random.NextDouble() < errorRate)
        {
           input = bad_prefix + input;
        }

        int value = 0;
        try
        {
            value = Int32.Parse(input);
        }
        catch(FormatException)
        {
            value = -1; // we would do something here with a logger perhaps
        }
    }
    stopwatch.Stop();

    return stopwatch.Elapsed;
}

/// Rate of errors in user input
/// Total time taken
public static TimeSpan TimeTryParse(double errorRate, int seed, int count)
{
    Stopwatch stopwatch = new Stopwatch();
    Random random = new Random(seed);
    string bad_prefix = @"X";

    stopwatch.Start();
    for(int ii = 0; ii < count; ++ii)
    {
        string input = random.Next().ToString();
        if (random.NextDouble() < errorRate)
        {
           input = bad_prefix + input;
        }

        int value = 0;
        if (!Int32.TryParse(input, out value))
        {
            value = -1; // we would do something here with a logger perhaps
        }
    }
    stopwatch.Stop();

    return stopwatch.Elapsed;
}

public static void TimeStringParse()
{
    double errorRate = 0.1; // 10% of the time our users mess up
    int count = 10000; // 10000 entries by a user

    TimeSpan trycatch = TimeTryCatch(errorRate, 1, count);
    TimeSpan tryparse = TimeTryParse(errorRate, 1, count);

    Console.WriteLine("trycatch: {0}", trycatch);
    Console.WriteLine("tryparse: {0}", tryparse);
}


亚里士多德从来没有通过实验来弄脏手.惭愧,羞耻.你需要断言一些事情显然是真的.这是互联网的方式!

2> Kev..:

虽然我没有亲自描述不同的方式,但这个章节有:

http://blogs.msdn.com/ianhu/archive/2005/12/19/505702.aspx



3> Patrick Desj..:

Try-Catch总是会慢一些.TryParse会更快.

IF和TryParse是相同的.


要完全清楚,如果解析失败,Try-Catch只会更慢; 不抛出/捕获异常不需要任何费用.
推荐阅读
mobiledu2402852357
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有