当前位置:网站首页>The difference and efficiency comparison of three methods of C # conversion integer

The difference and efficiency comparison of three methods of C # conversion integer

2022-07-26 16:26:00 A bowl of glutinous rice balls

C# The difference between the three ways of converting integer

Three ways of conversion :
1. Coercive transformation (int)
2. Convert.ToInt32()
3. int.Parse(string) or int.TryParse(string, out int)

Three types of integer conversion :
a. Floating point to integer

 Insert picture description here
From the prompt of error reporting int.Parse() or int.TryParse(string, out int) These two methods can only convert string numbers to integers , Floating point numbers cannot be converted to integers .
 Insert picture description here
According to the results above ,(int) Strong go Intercepted integer part ,Convert.ToInt32() Follow the principle of rounding


b. String number to integer

 Insert picture description here
It can be seen from the error report ,(int) Strong conversion cannot convert a string number to an integer
 Insert picture description here
Convert.ToInt32(),int.Parse(string) or int.TryParse(string, out int) Can convert string numbers to integers


c. Character to integer

 Insert picture description here
From the prompt of error reporting int.Parse() or int.TryParse(string, out int) These two methods cannot convert characters to integers
 Insert picture description here
(int) Strong go 、Convert.ToInt32 You can convert characters to integers


Sum up :
Floating point numbers can be converted to integers :(int) Strong go Truncate the integral part ,Convert.ToInt32() Obey rounding
Characters can be converted into integers :(int) Strong go ,Convert.ToInt32()
You can convert string numbers to integers :Convert.ToInt32(),int.Parse(string) , int.TryParse(string, out int)


C# The efficiency comparison of the three methods of converting integer

1. (int) Forced conversion sum Convert.ToInt32 Comparison of , Because both can convert character type to integer type

 Insert picture description here
 Insert picture description here
 Insert picture description here
Because considering that the two methods may have different efficiency on different platforms , So they are Unity On the engine and VS2017 Test on editor .
( There was a little episode during the test , You will find that there is something wrong with the code , Is the variable in the loop i The value of will be changed , Create an infinite cycle , The result is stuck , However, this will not affect the final result of the test , Later, I also corrected the code , The conclusion of the test is the same ). It can be concluded from the above results :(int) The forced transfer efficiency is higher than Convert.ToInt32.


2. int.Parse(string) , int.TryParse(string, out int),Convert.ToInt32() Efficiency comparison , Because all three can convert string numbers to integers

 Insert picture description here
 Insert picture description here
 Insert picture description here
From the results of the above figure, we can draw a conclusion :
stay Unity Engine efficiency from high to low :int.Parse(string) , int.TryParse(string, out int),Convert.ToInt32()
stay VS Control the efficiency of Taishan from high to low :Convert.ToInt32(),int.Parse(string) , int.TryParse(string, out int)
The test results are a little different , Possible and Unity The test environment of is related to the engine .


There was something wrong with the program during the last test , The number of iterations may be too few , Data comparison may be accidental , So I tested and compared , Add the number of iterations to 1000 and 10000, The conclusion drawn from the comparison of the results is still the same as last time .

Finally, let's summarize :

Floating point to integer

  1. (int) Strong go Truncate the integral part ,Convert.ToInt32() Follow the principle of rounding .
  2. Choose according to your needs .

Character to integer

  1. (int) Strong go ,Convert.ToInt32().
  2. stay Unity Medium test and VS The conclusions of the console test are consistent ,(int) The forced transfer efficiency is higher than Convert.ToInt32.

Integer numeric string to integer

  1. Convert.ToInt32(),int.Parse(string) or int.TryParse(string, out int).
  2. stay Unity Medium test and VS The conclusion of console test is a little different ,
    Unity Medium efficiency from high to low :
    int.Parse(string) , int.TryParse(string, out int),Convert.ToInt32();
    stay VS The efficiency in the console goes from high to low :
    Convert.ToInt32(),int.Parse(string) , int.TryParse(string, out int).
  3. If most of them are in Unity Use in , When converting an integer numeric string to an integer , stay int.Parse(string) , int.TryParse(string, out int) Choose between the two according to your needs .
原网站

版权声明
本文为[A bowl of glutinous rice balls]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207261610178130.html