当前位置:网站首页>C#转整型的三种方式的区别以及效率对比
C#转整型的三种方式的区别以及效率对比
2022-07-26 16:10:00 【汤圆一碗】
C#转整型的三种方式的区别
三种转换方式:
1. 强制转换(int)
2. Convert.ToInt32()
3. int.Parse(string) 或 int.TryParse(string, out int)
三种类型转整型:
a.浮点型转整型

由报错的提示可知int.Parse() 或 int.TryParse(string, out int)这两种方法只能将字符串数字转为整型,不能将浮点数转为整型。
由上图结果可知,(int)强转 截取整数了部分,Convert.ToInt32()遵从四舍五入的原则
b.字符串数字转整型

由报错可知,(int)强转不能将字符串数字转为整型
Convert.ToInt32(),int.Parse(string) 或 int.TryParse(string, out int)都可以将字符串数字转为整型
c. 字符转整型

由报错的提示可知int.Parse() 或 int.TryParse(string, out int)这两种方法不能将字符转为整型
(int)强转、Convert.ToInt32可以将字符转为整型
总结下:
可以浮点数转整数:(int)强转 截取整数部分,Convert.ToInt32()遵从四舍五入
可以字符转整型:(int)强转,Convert.ToInt32()
可以字符串数字转整型:Convert.ToInt32(),int.Parse(string) , int.TryParse(string, out int)
C#转整型三种方式的效率对比
1. (int)强转和Convert.ToInt32的对比,因为两者都能字符型转整型



因为考虑到两种方法在不同的平台上可能效率会有不同,所以分别在Unity引擎上和VS2017编辑器上测试。
(测试的时候出现了一点小插曲,就是你会发现这个代码其实写的有问题,就是循环里面变量i的值会被改变,造成无限循环了,结果处于卡死的状态,不过这个不会影响测试的最终结果,后来也改正了代码,测试得出的结论是一样的)。由上图结果可得出结论:(int)强转效率高于Convert.ToInt32。
2. int.Parse(string) , int.TryParse(string, out int),Convert.ToInt32()效率对比,因为三者都能将字符串数字转为整型



由上图的结果得出结论:
在Unity引擎上效率由高到低:int.Parse(string) , int.TryParse(string, out int),Convert.ToInt32()
在VS控制台山效率由高到低:Convert.ToInt32(),int.Parse(string) , int.TryParse(string, out int)
测试结果有点不同,可能与Unity的测试环境与引擎有关吧。
由于上次测试的时候程序写的有点问题,遍历的次数可能太少了,数据对比可能有偶然性,于是又测试对比了下,把遍历次数加到1000和10000,得出的结果对比得出的结论仍然与上次相同。
最后再总结:
浮点型转整型:
- (int)强转 截取整数部分,Convert.ToInt32()遵从四舍五入原则。
- 按需求选用。
字符转整型:
- (int)强转,Convert.ToInt32()。
- 在Unity中测试和VS控制台测试得出的结论都一致,(int)强转效率高于Convert.ToInt32。
整型数字字符串转整型:
- Convert.ToInt32(),int.Parse(string) 或 int.TryParse(string, out int)。
- 在Unity中测试和VS控制台测试得出的结论有点不一样,
Unity中效率由高到低:
int.Parse(string) , int.TryParse(string, out int),Convert.ToInt32();
在VS控制台中效率由高到低:
Convert.ToInt32(),int.Parse(string) , int.TryParse(string, out int)。 - 如果大多数在Unity中使用,在整型数字字符串转整型的时候,在int.Parse(string) , int.TryParse(string, out int)两种中按需求选择吧。
边栏推荐
- Pat grade a 1048 find coins
- Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click
- Collection of open source expert opinions on trusted privacy computing framework "argot"
- TKE集群节点max-pod是如何配置的
- 2022 latest Beijing Construction Safety Officer simulation question bank and answers
- Jointly discuss the opening of public data, and the "digital document scheme" appeared at the digital China Construction Summit
- 修改mysql数据库root用户的密码
- Build resume editor based on Nocode
- SQL statement -- single line comment and multi line comment
- Test cases should never be used casually, recording the thinking caused by the exception of a test case
猜你喜欢

Implementation of personalized healthy diet recommendation system based on SSM

【物理模拟】最简单的shape matching的原理与实践

综合设计一个OPPE主页--导航栏的设计

Pat class a 1047 student list for course

PAT甲级 1047 Student List for Course

SQL statement -- single line comment and multi line comment

vscode批量删除

DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库

Some cutting-edge research work sharing of SAP ABAP NetWeaver containerization

Re7:读论文 FLA/MLAC Learning to Predict Charges for Criminal Cases with Legal Basis
随机推荐
6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
Implementation of personalized healthy diet recommendation system based on SSM
Clojure Web 开发-- Ring 使用指南
The solution to the display disorder of several events files in the tensorboard
Interface test for quick start of JMeter
Summary of key knowledge of C language
Pat class a 1047 student list for course
Internet Protocol
Modify the password of the root user of MySQL database
物联网工业级串口转WiFi转网口转以太网模块的选型
From SiCp to LISP video replay
2022 latest Beijing Construction Safety Officer simulation question bank and answers
基于sisotool极点配置PI参数及基于Plecs的三相电压源逆变器仿真
The difference between oncreate and onrestoreinstancestate recovery data of activity
Application of workflow engine in vivo marketing automation
绘制漂亮的中学操场轮廓,生成带经纬度数据
Build resume editor based on Nocode
Acl-ijcai-sigir top conference paper report meeting (AIS 2022) Note 3: dialogue and generation
综合设计一个OPPE主页--明星机型的设计
Pat grade a 1049 counting ones