在c#win中获取下一个char值的最快方法。形成 [英] Fastest way to get next char value in c# win. form

查看:110
本文介绍了在c#win中获取下一个char值的最快方法。形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能给我一个在c#win中获得下一个char值的最快方法。表格



例如: -

输入: - a

输出: - b



这里有一个棘手的部分,解决方案应该在{00:00:00.0000020}秒内执行。



我的代码到现在为止[这里我不需要下一个char是逗号,引号,括号]

Can somebody give me the fastest way to get next char value in c# win. form

example:-
Input :- a
Output :- b

Here comes the tricky part that the solution should be executed within {00:00:00.0000020} sec.

my code till now is [here i don't need next char which are comma, quotes, brackets]

private static char NextSeries(char LastChar)
{
    char NewChar = ' ';
    try
    {
        Again:
        NewChar = LastChar++;
        if(NewChar == '\'' || NewChar == '"' || NewChar == '(' || NewChar == ')' || NewChar == ',')
                {
                    LastChar = NewChar;
                    goto Again;
                }
            }
            catch (Exception ex) { }
            return NewChar;
        }





提前谢谢



Thanks in advance

推荐答案

您是否考虑过postfix增量运算符? :

Have you considered the postfix increment operator:
char c = 'a';
Stopwatch s1 = new Stopwatch();
s1.Start();
for (int i = 0; i < 1000000; i++)
    {
    c++;
    }
s1.Stop();
Console.WriteLine(s1.ElapsedMilliseconds);

在调试器中给出输出3或4,因此没有优化。哪个确实在你的极限范围内!

它在你的系统中运行的速度究竟有多快,这取决于当时的处理器速度,内核数量和负载。

Gives me an output of "3" or "4", in the debugger, so without optimisations. Which is well and truly within your limit!
Exactly how fast it runs in your system is down to the processor speed, cores count and load at the time.


我怀疑你需要处理0x0000到0x1F8FF范围内的所有Unicode字符,我认为在这里获得快速查找的正确策略是预先计算每个字符输入的可能字符输出...而不是按字符计算和测试。



这是一个示例实现:
I doubt you will need to handle all the Unicode characters in the range 0x0000 to 0x1F8FF, and I think the correct strategy for getting speedy look-up here is to pre-compute the possible character output for each character input ... not to calculate, and test, character by character.

Here's a sample implementation:
private Dictionary<char,> nextCharLookUp = new Dictionary<char,>();
private List<char> excludeChars;

private void buildCharLists(int startChar, int endChar)
{
    excludeChars = new List<char>
    {
        '\'','"','(',')',','
    };

    for (int i = startChar; i <= endChar; i++)
    {
        char c1 = Convert.ToChar(i);
        char c2 = Convert.ToChar(i + 1);

        // forbidden character ?
        if (excludeChars.Contains(c1)) continue;

        // c1 is valid, but, what if the next char is invalid ?
        if (excludeChars.Contains(c2))
        {
            // this may not be what you want ?
            nextCharLookUp.Add(c1,c1);
        }
        else
        {
            // just fine here ...
            nextCharLookUp.Add(c1,c2);
        }
    }
}

显然,如果您可以在解析文件/字符串之前对其进行预处理,以删除任何您不想处理的字符,节省时间。



留给的是编写一些代码,使用这里构建的'nextCharLookUp Dictionary来解析文件/字符串数据:哦,是的,你仍然需要测试禁用字符,并做正确的事。



.NET中的字典查找是高度优化的,即,快速(引擎盖下的.NET通用词典是哈希表)。

Obviously, if you can pre-process your file/string before parsing it to remove any characters you don't want to handle, you'll save time.

Left for you is to write some code that uses the 'nextCharLookUp Dictionary built here to parse file/string data: oh yes, you'll still have to test for "forbidden" characters, and do the right thing.

Dictionary look-up in .NET is highly optimized, i.e., fast (under-the-hood .NET Generic Dictionaries are Hash-Tables).


这篇关于在c#win中获取下一个char值的最快方法。形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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