持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第6天,点击查看活动详情 c# 基础数据类型格式及范围
bool -> System.Boolean (布尔型,其值为 true 或者 false)
decimal->System.Decimal(表示十进制数,占16个字节)
byte -> System.Byte (字节型,占 1 字节,表示 8 位正整数,范围 0 ~ 255)
sbyte -> System.SByte (带符号字节型,占 1 字节,表示 8 位整数,范围 -128 ~ 127)
char -> System.Char (字符型,占有两个字节,表示 1 个 Unicode 字符)
short -> System.Int16 (短整型,占 2 字节,表示 16 位整数,范围 -32,768 ~ 32,767)
ushort -> System.UInt16 (无符号短整型,占 2 字节,表示 16 位无符号整数,范围 0 ~ 65,535)
uint -> System.UInt32 (无符号整型,占 4 字节,表示 32 位无符号整数,范围 0 ~ 4,294,967,295)
int -> System.Int32 (整型,占 4 字节,表示 32 位整数,范围 -2,147,483,648 到 2,147,483,647)
float -> System.Single (单精度浮点型,占 4 个字节,范围-3.40282347E+38F到3.40282347E+38F)
ulong -> System.UInt64 (无符号长整型,占 8 字节, 表示 64 位无符号整数,范围 0 ~ 大约 10 的 20 次方)
long -> System.Int64 (长整型,占 8 字节, 表示 64 位有符号整数,范围大约 -(10 的 19) 次方 到 10 的 19 次方)
double -> System.Double (双精度浮点型,占8 个字节,范围(-1.7976931348623157E+308,1.7976931348623157E+308))
- c# Duff优化for循环耗时
看Js高级编程的时候 发现了一个叫duff 可以优化for循环的
感觉比较有趣简单 写了一个c#版的
winfrom的三个button的代码如下 如果要效果明显 可以增大for 循环长量的值
public const int Counts= 10000000;
private void button1_Click(object sender, EventArgs e)
{
double A = 0;
List<int> Old = new List<int>();
for (int j= 0; j < Counts; j++)
{
Old.Add(j);
}
Stopwatch sw = new Stopwatch();
sw.Start();
//
int iterations =(int)Math.Ceiling((Old.Count*1.0 / 8));
int startAt = Old.Count % 8;
var i = 0;
do
{
switch (startAt)
{
case 0:
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
break;
case 7:
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
break;
case 6:
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
break;
case 5:
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
break;
case 4:
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
break;
case 3:
A += (Old[i++]);
A += (Old[i++]);
A += (Old[i++]);
break;
case 2:
A += (Old[i++]);
A += (Old[i++]);
break;
case 1:
A += (Old[i++]);
break;
}
startAt = 0;
} while (--iterations > 0);
sw.Stop();
TimeSpan ts2 = sw.Elapsed;
Console.WriteLine(A.ToString());
Console.WriteLine("Stopwatch总共花费{0}ms.", ts2.TotalMilliseconds);
}
private void button2_Click(object sender, EventArgs e)
{
double A2 = 0;
List<int> Old2 = new List<int>();
for (int j = 0; j < Counts; j++)
{
Old2.Add(j);
}
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < Old2.Count; i++)
{
A2 += Old2[i];
}
sw.Stop();
TimeSpan ts2 = sw.Elapsed;
Console.WriteLine(A2.ToString());
Console.WriteLine("Stopwatch总共花费{0}ms.", ts2.TotalMilliseconds);
}
private void button3_Click(object sender, EventArgs e)
{
double A3 = 0;
List<int> Old3 = new List<int>();
for (int j = 0; j < Counts; j++)
{
Old3.Add(j);
}
Stopwatch sw = new Stopwatch();
sw.Start();
int a = Old3.Count;
for (int i = 0; i < a; i++)
{
A3 += Old3[i];
}
sw.Stop();
TimeSpan ts2 = sw.Elapsed;
Console.WriteLine(A3.ToString());
Console.WriteLine("Stopwatch总共花费{0}ms.", ts2.TotalMilliseconds);
}