求效率最高算法,取四个整数中的最大值
比如有a,b,c,d四个整数,我们需要获得其中的最大值,最简单莫过于数组循环或者简单地如下用3个if;但是应该有更高效的算法,谁能指教:)
多谢
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;
}
--------------------编程问答--------------------
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; --------------------编程问答-------------------- 楼上的可以。 --------------------编程问答-------------------- 选择排序法,最好不要用系统内的函数(max,min)
因为那是经过多层封装之后的函数 --------------------编程问答-------------------- 效率很低的 --------------------编程问答-------------------- 对 选择排序 直接比对 应该是最快
你用任何函数他最终还是要去比对、、、
所以直接比对是最快的方法~ --------------------编程问答-------------------- 没有具体的比较过 所以关注一下
--------------------编程问答-------------------- --------------------编程问答-------------------- int d []...
int max=d[0];
for(....)
{
if(max<d[i])max=d[i];
}
最大值:max; --------------------编程问答-------------------- 看来需要实际测试一下啊? --------------------编程问答--------------------
up --------------------编程问答--------------------
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次赋值
稍微改进一点
--------------------编程问答-------------------- 3元运算的效率还不如3个if快呢! --------------------编程问答--------------------
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
--------------------编程问答-------------------- protected int large(int a, int b, int c, int d) {
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次赋值会不会比三元运算快呢! --------------------编程问答-------------------- 路过,学习了! --------------------编程问答-------------------- 这个还真没想过,长见识了 --------------------编程问答--------------------
以在13楼给出测试结果了。3目运算符是最快的。 --------------------编程问答-------------------- protected int large(int a, int b, int c, int d) {
if (a < b) a = b;
if (c < d) c = d;
if (a < c)
return c;
else
return a;
}
--------------------编程问答-------------------- protected int large(int a, int b, int c, int d) {
if (a < b) a = b;
if (c < d) c = d;
return a > c ? a : c;
} --------------------编程问答-------------------- 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次比较
}
强烈推荐,?运算效率明显高于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的差距在哪? --------------------编程问答--------------------
int max(int a, int b, int c, int d)三次比较,无赋值.
{
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;
}
只不过可读性囧... --------------------编程问答--------------------
这个看着比较简洁。呵呵。不过3目运算比人 用的还真是比较少哈。 --------------------编程问答--------------------
int max(int a, int b, int c, int d)上一程序的3目运算符版...
{
return a > b ? (a > c ? (a > d ? a : d) : (c > d ? c : d)) : (b > c ? (b > d ? b : d) : (c > d ? c : d));
}
可读性继续直线下降囧.... --------------------编程问答-------------------- 恩。要说速度,25楼的纯问号表达式的最快。 --------------------编程问答-------------------- mark --------------------编程问答-------------------- 。。。 --------------------编程问答-------------------- 三目、IF性能都一样
13楼的代码,你换一个运行次序看看就知道了
性能差别只是CPU资源后面被进程抢占得更多。
这个没有优化的必要
一亿次差距在0.1秒没有意义
这个问题如果变成:1亿个数找到4个重复最多的数还有点研究的价值。 --------------------编程问答--------------------
最多也只能优化到这个程度了,而且优化的思想也很有意义.
再优化就只能嵌汇编了 --------------------编程问答-------------------- 从来不做这样没有意义的事 --------------------编程问答-------------------- 初学者路过,望多多指教 --------------------编程问答-------------------- 清清...虽然不知道从il到asm的代码性能如何,
但是单从生成的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--------------------编程问答-------------------- 每天回帖即可获得10分可用分!小技巧 呵呵 --------------------编程问答-------------------- 编译器也会进化的。下午出一个算法题来玩玩,这个帖子可以忽略。 --------------------编程问答-------------------- shit ,看得头疼, --------------------编程问答--------------------
{
ldarg a
ldarg b
bgt amax
ldarg b
ret
amax:
ldarg a
ret
}
return Math.Max(Math.Max(a, b), Match.Max(c, d));
补充:.NET技术 , C#