Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 29-Oct-14 1:46am
v3
Comments
BillWoodruff 29-Oct-14 6:55am    
Are you handling only a limited range of the 256 chars possibly represented in one byte ? Like, for example, only a~z,0~9,A~Z ?
agent_kruger 29-Oct-14 7:47am    
sorry sir forgot to paste full code sorry for that and please see if you can help me with my new code and i am not using 256 char but all possible char

Have you considered the postfix increment operator:
C#
char c = 'a';
Stopwatch s1 = new Stopwatch();
s1.Start();
for (int i = 0; i < 1000000; i++)
    {
    c++;
    }
s1.Stop();
Console.WriteLine(s1.ElapsedMilliseconds);
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.
 
Share this answer
 
Comments
agent_kruger 29-Oct-14 7:46am    
sorry sir forgot to paste full code sorry for that and please see if you can help me with my new code
OriginalGriff 29-Oct-14 8:00am    
I'm not even looking at that! "goto"? Seriously?
agent_kruger 29-Oct-14 8:01am    
sir i have to call "goto" as if the char is coming which i don't need then i have to get new char.
OriginalGriff 29-Oct-14 8:51am    
How long have you been programming?
Is there something wrong with a "while" loop?
And if you want speed, don't use "if": use a switch, or set up a lookup table to specify the character translation and forget the loop altogether!
agent_kruger 29-Oct-14 8:54am    
look table? u mean storing all char before hand a container?
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:
C#
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);
        }
    }
}
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).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900