当前位置:网站首页>C # generics and performance comparison
C # generics and performance comparison
2022-07-08 00:22:00 【A descendant of the sun】
What is generics ? Generally speaking, it is the type of uncertainty .
When to use generics ? When we want to use a method to receive many different types of parameters, we can often use .
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
result 
In other words, the performance of ordinary methods is greater than that of generic methods of specified types, and greater than that of generic methods of unspecified types
Sometimes I really envy , Envy some people can never forget , We can draw inferences from one example , And I often have to read the same information many times to really understand .
But fortunately , Many things can be changed and obtained by hard work , Knock more code , Your skills will gradually improve , Run more , Your figure will get better , Listen to books more , I can also be full of experience .
come on. , Over the weekend , You should be better and better , This need not be seen by others , But you must know .
边栏推荐
猜你喜欢

QT and OpenGL: loading 3D models using the open asset import library (assimp) - Part 2

new和delete的底层原理以及模板

How to learn a new technology (programming language)

Daily question brushing record (16)

Relevant methods of sorting arrays in JS (if you want to understand arrays, it's enough to read this article)

【编程题】【Scratch二级】2019.12 飞翔的小鸟

Robomaster visual tutorial (1) camera

Play sonar

【编程题】【Scratch二级】2019.09 绘制雪花图案

QT adds resource files, adds icons for qaction, establishes signal slot functions, and implements
随机推荐
哪个券商公司开户佣金低又安全,又靠谱
深潜Kotlin协程(二十三 完结篇):SharedFlow 和 StateFlow
Go learning notes (1) environment installation and hello world
How does the markdown editor of CSDN input mathematical formulas--- Latex syntax summary
某马旅游网站开发(登录注册退出功能的实现)
Installation and configuration of sublime Text3
Qt添加资源文件,为QAction添加图标,建立信号槽函数并实现
Play sonar
什么是负载均衡?DNS如何实现负载均衡?
Trust orbtk development issues 2022
数据库查询——第几高的数据?
3年经验,面试测试岗20K都拿不到了吗?这么坑?
STM32F1与STM32CubeIDE编程实例-旋转编码器驱动
单机高并发模型设计
全自动化处理每月缺卡数据,输出缺卡人员信息
[basis of recommendation system] sampling and construction of positive and negative samples
Teach you to make a custom form label by hand
Tools for debugging makefiles - tool for debugging makefiles
Visual Studio Deployment Project - Create shortcut to deployed executable
What if the testing process is not perfect and the development is not active?