简体   繁体   English

将int转换为char的最快方法是什么

[英]What is the fastest way to convert int to char

What is the fastest way to convert int to char? 将int转换为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. 我有多个不同的int值,必须将其转换为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.ToChar方法和上述转换方法之间进行快速性能测试 ,我发现65535测试( 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. 在最佳状态下,cast在大约一半的转换时间内为我运行。

The implementation of Convert.ToChar as found in Reference Source reveals the time consumer: 参考源中提供Convert.ToChar的实现揭示了时间消耗者:

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 . 强制执行这些检查的一个不错的选择是使用ushort而不是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. 显然,可能达到或不可能达到,但是具有相同的最大值,这意味着您将在编译时检查以前取决于ToChar的性能。

Convert.ToChar eventually performs an explicit conversion as (char)value , where value is your int value. Convert.ToChar最终将显式转换为(char)value ,其中value是您的int值。 Before doing so, it checks to ensure value is in the range 0 to 0xffff , and throws an OverflowException if it is not. 在这样做之前,它将检查以确保值在0 to 0xffff范围内,如果不是,则抛出OverflowException 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 . 额外的方法调用,值/边界检查和OverflowException可能会有用,但是如果没有用,如果仅使用(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 (char)122是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 . 问题是为什么您需要将很多int转换为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 . 在所有其他情况下,都为int分配了内存,为char分配了内存。 Thus the best that can be achieved is simply copy the int to the char address. 因此,可以实现的最好结果就是将int复制到char地址。

However this code is 100% faster, since no code is run at all. 但是,由于根本不运行任何代码,因此该代码快100%。

[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 https://msdn.microsoft.com/zh-cn/library/aa288471%28v=vs.71%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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