当前位置:网站首页>C# 泛型及性能比较
C# 泛型及性能比较
2022-07-07 22:17:00 【太阳的后裔】
什么是泛型?通俗的说就是不确定的类型。
什么时候用到泛型?当我们想要使用一个方法接收多种不同的类型的参数的时候我们常常能够用到。
public static void Show()
{
Console.WriteLine("****************Monitor******************");
{
int iValue = 12345;
long commonSecond = 0;
long objectSecond = 0;
long genericSecond = 0;
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100_000_000; i++)
{
ShowInt(iValue);
}
watch.Stop();
commonSecond = watch.ElapsedMilliseconds;
}
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100_000_000; i++)
{
ShowObject(iValue);
}
watch.Stop();
objectSecond = watch.ElapsedMilliseconds;
}
{
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < 100_000_000; i++)
{
Show<int>(iValue);
}
watch.Stop();
genericSecond = watch.ElapsedMilliseconds;
}
Console.WriteLine("commonSecond={0},objectSecond={1},genericSecond={2}"
, commonSecond, objectSecond, genericSecond);
}
}
#region PrivateMethod
private static void ShowInt(int iParameter)
{
//do nothing
}
private static void ShowObject(object oParameter)
{
//do nothing
}
private static void Show<T>(T tParameter)
{
//do nothing
}
#endregion
结果
也就是说普通方法的性能大于指定类型的泛型方法大于未指定类型的泛型方法
偶尔真的很羡慕,羡慕某些人可以过目不忘,可以举一反三,而我同一份资料往往都要看多次才能真正的看懂。
但幸运的是,很多东西真的靠努力是可以改变和得到的,多敲敲代码,自己的技术就会慢慢提高,多跑跑步,身材就会变的更好,多听听书,自己也能满腹经纶。
加油,周末,你要越来越好,这不用别人看得出来,但是自己一定要知道。
边栏推荐
- 一鍵免費翻譯300多頁的pdf文檔
- Anaconda+pycharm+pyqt5 configuration problem: pyuic5 cannot be found exe
- 52歲的周鴻禕,還年輕嗎?
- 数据库查询——第几高的数据?
- 从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
- Introduction to programming hardware
- Using Google test in QT
- “一个优秀程序员可抵五个普通程序员”,差距就在这7个关键点
- Go learning notes (1) environment installation and hello world
- 自动化测试:Robot FrameWork框架90%的人都想知道的实用技巧
猜你喜欢
【编程题】【Scratch二级】2019.03 绘制方形螺旋
Using Google test in QT
10 schemes to ensure interface data security
一鍵免費翻譯300多頁的pdf文檔
Connect diodes in series to improve voltage withstand
Introduction to programming hardware
52岁的周鸿祎,还年轻吗?
3年经验,面试测试岗20K都拿不到了吗?这么坑?
One click free translation of more than 300 pages of PDF documents
The function is really powerful!
随机推荐
Reading notes 004: Wang Yangming's quotations
Install sqlserver2019
Development of a horse tourism website (realization of login, registration and exit function)
[programming problem] [scratch Level 2] draw ten squares in December 2019
Development of a horse tourism website (optimization of servlet)
Two small problems in creating user registration interface
Benchmarking Detection Transfer Learning with Vision Transformers(2021-11)
自动化测试:Robot FrameWork框架90%的人都想知道的实用技巧
Usage of limit and offset (Reprint)
DataGuard active / standby cleanup archive settings
Database query - what is the highest data?
Basic learning of SQL Server -- creating databases and tables with the mouse
Enterprise application demand-oriented development of human resources department, employee attendance records and paid wages business process cases
Stm32f1 and stm32cubeide programming example - rotary encoder drive
Is Zhou Hongyi, 52, still young?
光流传感器初步测试:GL9306
爬虫实战(八):爬表情包
SQL connection problem after downloading (2)
The result of innovation in professional courses such as robotics (Automation)
How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?