当前位置:网站首页>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

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 .
According to the results above ,(int) Strong go Intercepted integer part ,Convert.ToInt32() Follow the principle of rounding
b. String number to integer

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

From the prompt of error reporting int.Parse() or int.TryParse(string, out int) These two methods cannot convert characters to integers 
(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



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



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 :
- (int) Strong go Truncate the integral part ,Convert.ToInt32() Follow the principle of rounding .
- Choose according to your needs .
Character to integer :
- (int) Strong go ,Convert.ToInt32().
- 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 :
- Convert.ToInt32(),int.Parse(string) or int.TryParse(string, out int).
- 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). - 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 .
边栏推荐
- 2022年全国最新消防设施操作员(高级消防设施操作员)考试试题及答案
- From SiCp to LISP video replay
- 基于sisotool极点配置PI参数及基于Plecs的三相电压源逆变器仿真
- Collection of open source expert opinions on trusted privacy computing framework "argot"
- Summary of key knowledge of C language
- 2022年最新西藏建筑施工架子工(建筑特种作业)模拟考试试题及答案
- Comprehensively design an oppe homepage -- Design of star models
- VS2017打开项目提示需要迁移的解决方法
- Compiler analysis of clojure operation principle
- Finally, someone explained the red blue confrontation clearly
猜你喜欢
![[BJDCTF2020]Easy MD5](/img/6a/61a4b5624c33f1f334bea344cfa2c8.png)
[BJDCTF2020]Easy MD5

PAT甲级 1050 String Subtraction

FTP协议

MVC和ECS两种设计架构的初浅理解

2022 test questions and answers for the latest national fire facility operator (senior fire facility operator)

综合设计一个OPPE主页--顶部,头部的设计

Tdengine landed in GCL energy technology, with tens of billions of data compressed to 600gb

PAT甲级 1047 Student List for Course

微信小程序---网络数据请求

我的sql没问题为什么还是这么慢|MySQL加锁规则
随机推荐
Analyzing method and proc in Ruby
Linux安装mysql8.0.29详细教程
Final consistency distributed transaction TCC
哪本书才是编程领域的“九阴真经”
Activity之onCreate、onRestoreInstanceState恢复数据的区别
Tdengine landed in GCL energy technology, with tens of billions of data compressed to 600gb
最终一致性性分布式事务 TCC
Sword finger offer special assault edition day 11
PAT甲级 1046 Shortest Distance
C语言重点知识总结
Summary of key knowledge of C language
Alibaba cloud DMS MySQL cloud database report error, solve!!
Clojure operation principle bytecode generation
十周岁生日快乐,Clojure
ACL-IJCAI-SIGIR顶级会议论文报告会(AIS 2022)笔记3:对话和生成
研发效能的道与术 - 道篇
Specific practice cases of "card note taking method" in Siyuan
Octree establishes map and realizes path planning and navigation
From SiCp to LISP video replay
Clojure 运行原理之字节码生成篇