随着计算机硬件性能不断提高,我感觉自己越来越忽视算法的重要性了。今天在codility网站做了一道很简单的Demo算法题,发现性能差的不是一点半点的。
由于是Demo题,可以做多次,以下是我做2次的URL:
1. http://codility.com/demo/results/demoTY95EX-RGY/ [一开始就想用最简单的方式实现功能需求,但没想到性能这么差,分数才50(100分制),时间复杂度:O(N*N)]
2. http://codility.com/demo/results/demoWK3NEZ-5AB/ [这次实现还行,至少时间复杂度:O(N)]
我也在本地机器使用以下代码做了以下测试,发现相同数据,花费时间差距非常之大。
def solution(A): # http://codility.com/demo/results/demoTY95EX-RGY/ import math _min_diff = None for _inx in range(1, len(A)): _diff = int(math.fabs(sum(A[:_inx])-sum(A[_inx:]))) if _min_diff is None: _min_diff = _diff elif _min_diff > _diff: _min_diff = _diff return _min_diff def solution2(A): # http://codility.com/demo/results/demoWK3NEZ-5AB/ _min_diff = None _sum = sum(A) _left_sum = 0 for _inx in range(len(A)-1): _left_sum += A[_inx] _diff = _sum - _left_sum - _left_sum if _diff < 0: _diff = -_diff if _sum&0x01 == 0 and _diff == 0: #for even, minimal value is 0 _min_diff = 0 break elif _sum&0x01 == 1 and _diff == 1: #for odd, minimal value is 1 _min_diff = 1 break if _min_diff is None: _min_diff = _diff elif _min_diff > _diff: _min_diff = _diff return _min_diff def test(A): import time _start = time.time() _value = solution(A) _end = time.time() print "solution result: time=%f, value=%d" % (_end-_start, _value) _start = time.time() _value = solution2(A) _end = time.time() print "solution2 result: time=%f, value=%d" % (_end-_start, _value)
输出结果:
>>> A = [random.randint(-1000,1000) for _ in xrange(200)] >>> test(A) solution result: time=0.001000, value=14 solution2 result: time=0.000000, value=14 >>> A = [random.randint(-1000,1000) for _ in xrange(2000)] >>> test(A) solution result: time=0.077000, value=3 solution2 result: time=0.001000, value=3 >>> A = [random.randint(-1000,1000) for _ in xrange(2000)] >>> A = [random.randint(-1000,1000) for _ in xrange(20000)] >>> test(A) solution result: time=5.497000, value=24 solution2 result: time=0.007000, value=24 >>> A = [random.randint(-1000,1000) for _ in xrange(200000)] >>> test(A) solution result: time=667.812000, value=0 solution2 result: time=0.016000, value=0
A中有200,000元素的时候,算法1花费667秒,算法2才0.016秒。有点无语了,应该好好反省,今后在日常的开发中要更加重视算法(及数据结构)的选择。