首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

NlogN比N^2要慢?该如何解决

2012-02-10 
NlogN比N^2要慢?最大和序列问题有多种解法,据说动态规划的要比两重循环快。(详见附件)algo_cmp_windows.cpp

NlogN比N^2要慢?
最大和序列问题有多种解法,据说动态规划的要比两重循环快。(详见附件)

algo_cmp_windows.cpp在一台P4 3.0G Hz上的结果是(尽管显示的不对),
CPU frequency is 3579545 Hz

Cubic time:
8
7
7
8
9
9
10
11
12
13
15
17
19

Quadratic time:
7
6
6
6
7
7
8
7
8
8
9
9
9

N log N time:
7
7
8
8
8
9
9
9
10
10
11
11
12

Linear time:
6
6
6
6
6
6
6
6
6
6
6
6
6

(在其他机器上CPU频率和计数都是正确的,这个可以忽略)

Linux版的algo_cmp_linux.cpp(编译时需要-l rt)在一台SMP上的结果是,
cpu MHz : 2994.204
cpu MHz : 2994.204

Cubic time:
Size: 3, Maximun sum is 10, Time: 1000 nano seconds.
Size: 4, Maximun sum is 19, Time: 1000 nano seconds.
Size: 5, Maximun sum is 19, Time: 1000 nano seconds.
Size: 6, Maximun sum is 19, Time: 1000 nano seconds.
Size: 7, Maximun sum is 35, Time: 1000 nano seconds.
Size: 8, Maximun sum is 35, Time: 1000 nano seconds.
Size: 9, Maximun sum is 35, Time: 2000 nano seconds.
Size: 10, Maximun sum is 35, Time: 2000 nano seconds.
Size: 11, Maximun sum is 35, Time: 3000 nano seconds.
Size: 12, Maximun sum is 129, Time: 3000 nano seconds.
Size: 13, Maximun sum is 129, Time: 4000 nano seconds.
Size: 14, Maximun sum is 129, Time: 5000 nano seconds.
Size: 15, Maximun sum is 129, Time: 5000 nano seconds.

Quadratic time:
Size: 3, Maximun sum is 10, Time: 1000 nano seconds.
Size: 4, Maximun sum is 19, Time: 1000 nano seconds.
Size: 5, Maximun sum is 19, Time: 1000 nano seconds.
Size: 6, Maximun sum is 19, Time: 1000 nano seconds.
Size: 7, Maximun sum is 35, Time: 1000 nano seconds.
Size: 8, Maximun sum is 35, Time: 1000 nano seconds.
Size: 9, Maximun sum is 35, Time: 1000 nano seconds.
Size: 10, Maximun sum is 35, Time: 2000 nano seconds.
Size: 11, Maximun sum is 35, Time: 2000 nano seconds.
Size: 12, Maximun sum is 129, Time: 1000 nano seconds.
Size: 13, Maximun sum is 129, Time: 2000 nano seconds.
Size: 14, Maximun sum is 129, Time: 2000 nano seconds.
Size: 15, Maximun sum is 129, Time: 1000 nano seconds.

N log N time:
Size: 3, Maximun sum is 10, Time: 2000 nano seconds.
Size: 4, Maximun sum is 19, Time: 1000 nano seconds.
Size: 5, Maximun sum is 19, Time: 1000 nano seconds.
Size: 6, Maximun sum is 19, Time: 1000 nano seconds.
Size: 7, Maximun sum is 35, Time: 1000 nano seconds.
Size: 8, Maximun sum is 35, Time: 2000 nano seconds.
Size: 9, Maximun sum is 35, Time: 2000 nano seconds.
Size: 10, Maximun sum is 35, Time: 2000 nano seconds.
Size: 11, Maximun sum is 35, Time: 2000 nano seconds.
Size: 12, Maximun sum is 129, Time: 2000 nano seconds.
Size: 13, Maximun sum is 129, Time: 2000 nano seconds.
Size: 14, Maximun sum is 129, Time: 2000 nano seconds.
Size: 15, Maximun sum is 129, Time: 2000 nano seconds.

Linear time:
Size: 3, Maximun sum is 10, Time: 1000 nano seconds.
Size: 4, Maximun sum is 19, Time: 1000 nano seconds.
Size: 5, Maximun sum is 19, Time: 1000 nano seconds.
Size: 6, Maximun sum is 19, Time: 1000 nano seconds.
Size: 7, Maximun sum is 35, Time: 1000 nano seconds.
Size: 8, Maximun sum is 35, Time: 1000 nano seconds.
Size: 9, Maximun sum is 35, Time: 1000 nano seconds.
Size: 10, Maximun sum is 35, Time: 0 nano seconds.
Size: 11, Maximun sum is 35, Time: 0 nano seconds.
Size: 12, Maximun sum is 129, Time: 1000 nano seconds.
Size: 13, Maximun sum is 129, Time: 0 nano seconds.
Size: 14, Maximun sum is 129, Time: 1000 nano seconds.
Size: 15, Maximun sum is 129, Time: 1000 nano seconds.

总之maxSubSum3比maxSubSum2要慢?

[解决办法]
数据规模太小了,一次函数调用的代价都差不多同你计算部分的代价差不多了。


至少也得用上数百数千的数据测试一下吧
[解决办法]
是呀,历史上还出现过,对于同一问题,对于较大的数据,多项式算法不如指数算法的情况。

热点排行