简体   繁体   中英

What is the fastest way to convert int to char

What is the fastest way to convert int to char? I need a faster way because,

convertingchar = Convert.ToChar(intvalue);

is my slowest part in the program. I have multiple different int values, that have to be converted to char. My project is very big and I can't post my function. That's why I post this test function.

My Code so far.

char convertingchar = ' ';
...some code...
public void convert(int intvalue){
   convertingchar = Convert.ToChar(intvalue);
}

Running a quick performance test between your Convert.ToChar approach and the mentioned casting one, I find that 65535 tests ( char.MaxValue ):

Convert: 00:00:00.0005447 total
Cast:    00:00:00.0003663 total

At its best, cast was running for me in about half the time of convert.

The implementation of Convert.ToChar as found in Reference Source reveals the time consumer:

public static char ToChar(int value) {
    if (value < 0 || value > Char.MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Char"));
    Contract.EndContractBlock();
    return (char)value;
}

While these checks do certainly serve a purpose, your particular use-cases, especially in such a performance-critical situation, may not require them. That's up to you.

A nice alternative to enforce these checks would be to use a ushort rather than an int . Obviously that may or may not be attainable, but with the same maximum value, this means you'll get compile-time checking for what you were previously depending on ToChar to perform.

Convert.ToChar eventually performs an explicit conversion as (char)value , where value is your int value. Before doing so, it checks to ensure value is in the range 0 to 0xffff , and throws an OverflowException if it is not. The extra method call, value/boundary checks, and OverflowException may be useful, but if not, the performance will be better if you just use (char)value .

This will make sure everything is ok while converting but take some time while making sure of that,

convertingchar = Convert.ToChar(intvalue);

This will convert it without making sure everything is ok so less time,

convertingchar = (char)intvalue;

For example.

 Console.WriteLine("(char)122 is {0}", (char)122);

yields:

(char)122 is z


NOTE

Not related to question directly but if you feel that Conversion is slow then you might be doing something wrong. The question is why do you need to convert the lot of the int to char . What you are trying to achieve. There might be better way.

The fastest way, contrary to what the others have noted is to not run any code at all. In all the other cases, there is memory allocated for the int and memory allocated for the char . Thus the best that can be achieved is simply copy the int to the char address.

However this code is 100% faster, since no code is run at all.

[StructLayout(LayoutKind.Explicit)]
public struct Foo
{
    [FieldOffset(0)] 
    public int Integer;
    [FieldOffset(0)] 
    public char Char;
}

https://msdn.microsoft.com/en-us/library/aa288471%28v=vs.71%29.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM