int[] arr = {2,4,5,6 };
ArrayList list = new ArrayList(arr);
list.Sort();
int min = Convert.ToInt32(list[0]);
int max = Convert.ToInt32(list[list.Count - 1]);
Console.WriteLine("最大" + max +" 最小:"+min); 记得导入命名空间
using System.Collections;
ArrayList list = new ArrayList(arr);
list.Sort();
int min = Convert.ToInt32(list[0]);
int max = Convert.ToInt32(list[list.Count - 1]);
Console.WriteLine("最大" + max +" 最小:"+min); 记得导入命名空间
using System.Collections;
因为那是经过多层封装之后的函数
int max=d[0];
for(....)
{
if(max<d[i])max=d[i];
}
最大值:max;
protected int large(int a, int b, int c, int d) {
int r = a;//赋值
if (r < b) r = b;//比较,赋值
if (r < c) r = c;//比较,赋值
if (r < d) r = d;//比较,赋值
return r;
}
//最差情况:共3次比较,4次赋值 最好情况:3次比较2次赋值稍微改进一点protected int large(int a, int b, int c, int d)
{
int x = a > b ? a : b; //1次比较,1次赋值
int y = c > d ? c : d; //1次比较,1次赋值
return x > y ? x : y; //1次比较
}
稳定为3次比较2次赋值。
是么?
private void button1_Click(object sender, EventArgs e)
{
int tick = Environment.TickCount;
for (int i = 0; i < 100000000; i++)
{
large(8, 3, 9, 15);
}
int tick2 = Environment.TickCount - tick;
tick = Environment.TickCount;
for (int i = 0; i < 100000000; i++)
{
large2(8, 3, 9, 15);
}
tick = Environment.TickCount - tick;
MessageBox.Show(tick2.ToString()+Environment.NewLine+tick.ToString());
}protected int large(int a, int b, int c, int d)
{
int x, y;
if (a > b) x = a;
else x = b;
if (c > d) y = c;
else y = d;
if (x > y) return x;
else return y;
}protected int large2(int a, int b, int c, int d)
{
int x = a > b ? a : b; //1次比较,1次赋值
int y = c > d ? c : d; //1次比较,1次赋值
return x > y ? x : y; //1次比较
}[test1]
if :2703
三目 :2687
[test2]
if :2704
三目 :2687
[test3]
if :2704
三目 :2687
[test4]//非调试模式直接运行
if :1485
三目 :1453
[test5]
if :1500
三目 :1453
int r,x,y;
if (a < b) x = b;
else r = a;
if (c < d) y = d;
else y = c;
if (x < y) r = y;
else r = x;
return r;
}
这3次比较3次赋值会不会比三元运算快呢!
if (a < b) a = b;
if (c < d) c = d;
if (a < c)
return c;
else
return a;
}
if (a < b) a = b;
if (c < d) c = d;
return a > c ? a : c;
}
{
int x = a > b ? a : b; //1次比较,1次赋值
int y = c > d ? c : d; //1次比较,1次赋值
return x > y ? x : y; //1次比较
}
强烈推荐,?运算效率明显高于if等
public static int large(int a, int b, int c, int d)
{
int x, y;
if (a > b) x = a;
else x = b;
if (c > d) y = c;
else y = d;
if (x > y) return x;
else return y;
} public static int large2(int a, int b, int c, int d)
{
int x = a > b ? a : b; //1次比较,1次赋值
int y = c > d ? c : d; //1次比较,1次赋值
return x > y ? x : y; //1次比较
}
public static int large3(int a, int b, int c, int d)
{
if (a < b)
a = b;
if (a < c)
a = c;
if (a < d)
return d;
return a;
}这三种方法还是第三种快那么一点点,在我的电脑上循环了一亿次差距在0.1秒左右。
{
if (a > b)
if (a > c)
if (a > d)
return a;
else
return d;
else if (c > d)
return c;
else
return d;
else if (b > c)
if (b > d)
return b;
else
return d;
else if (c > d)
return c;
else
return d;
}三次比较,无赋值.
只不过可读性囧...
{
return a > b ? (a > c ? (a > d ? a : d) : (c > d ? c : d)) : (b > c ? (b > d ? b : d) : (c > d ? c : d));
}上一程序的3目运算符版...
可读性继续直线下降囧....
性能差别只是CPU资源后面被进程抢占得更多。这个没有优化的必要一亿次差距在0.1秒没有意义这个问题如果变成:1亿个数找到4个重复最多的数还有点研究的价值。
但是单从生成的il来看三目运算的确是要比if快的.
我写了两段程序: static int max1 (int a,int b)
{
if (a>b)
return a;
return b;
}
static int max2 (int a,int b)
{
return a>b?a:b;
}
反编译成IL(.net framework 4.0 csc编译器):
.method private hidebysig static int32 max1(int32 a, int32 b) cil managed
{
.maxstack 2
.locals init (
[0] int32 num,
[1] bool flag)
L_0000: nop
L_0001: ldarg.0
L_0002: ldarg.1
L_0003: cgt
L_0005: ldc.i4.0
L_0006: ceq
L_0008: stloc.1
L_0009: ldloc.1
L_000a: brtrue.s L_0010
L_000c: ldarg.0
L_000d: stloc.0
L_000e: br.s L_0014
L_0010: ldarg.1
L_0011: stloc.0
L_0012: br.s L_0014
L_0014: ldloc.0
L_0015: ret
}.method private hidebysig static int32 max2(int32 a, int32 b) cil managed
{
.maxstack 2
.locals init (
[0] int32 num)
L_0000: nop
L_0001: ldarg.0
L_0002: ldarg.1
L_0003: bgt.s L_0008
L_0005: ldarg.1
L_0006: br.s L_0009
L_0008: ldarg.0
L_0009: stloc.0
L_000a: br.s L_000c
L_000c: ldloc.0
L_000d: ret
}可以看到使用if的函数中间多了个bool变量的赋值,而且在正式跳转开始前多做了两次比较(cgt和ceq)不过我个人认为即使这样用极其晦涩难懂的代码换来的这两次比较一次赋值的速度提升还是不值得的.
而且两条函数都还极其蛋疼的弄了个中间变量num来保存结果.如果真想达到最快还不如用il:
.method private hidebysig static int32 max3(int32 a,int32 b) cil managed
{
ldarg a
ldarg b
bgt amax
ldarg b
ret
amax:
ldarg a
ret
}
return Math.Max(Math.Max(a, b), Match.Max(c, d));