还未发布过话题
  • import random
    
    def getCloseNumList(num):
        locList = dict(enumerate(map(lambda x:((x-1)/3,(x-1)%3),range(1,10)), 1))
        num_x, num_y = locList.get(num)
        ret = []
        for j in locList:
            if j!=num:
                j_x,j_y = locList.get(j)
                if abs(j_x-num_x)<=1 and abs(j_y-num_y)<=1:
                    ret.append(j)
        return ret
    
    def run(times):
        try:
            result = []
            for i in range(times):
                if i == 0:
                    last = random.choice(range(1, 10))
                    result.append(last)
                else:
                    choiceList = list(set(getCloseNumList(last)) - set(result))
                    last = random.choice(choiceList)
                    result.append(last)
            return result
        except:
            run(times)
    
    def loadTest():    #新增性能测试
        start = time.time()
        for i in xrange(10000):
            run(2)
        print "100000次2 : %f" % (time.time() - start)
    
        start = time.time()
        for i in xrange(10000):
            run(5)
        print "10000次5 : %f" % (time.time() - start)
    
        start = time.time()
        for i in xrange(5000):
            run(7)
        print "5000次7 : %f" % (time.time() - start)
    
        try:
            start = time.time()
            for i in xrange(2000):
                run(8)
            print "2000次8 : %f" % (time.time() - start)
        except Exception, e:
            print "2000次8 : 运行异常:%s" % (e.message)
    
        try:
            start = time.time()
            for i in xrange(1000):
                run(9)
            print "1000次9 : %f" % (time.time() - start)
        except Exception, e:
            print "1000次9 : 运行异常:%s" % (e.message)
    
    if __name__ == '__main__':
        times = raw_input("请输入节点数量:")
        if not times.isdigit() or int(times)>9 or int(times)<1:
            print "无效数字!"
            exit(1)
        print run(int(times))
        loadTest()
    

    性能测试结果: