三个数字的排序

以下程序为三个数字的排序,并计算所用时间。

 

#!/usr/bin/env python

def test1(x1=8,x2=5,x3=10,order=None):
    ''' default is ascending order '''
    temp1=x1
    temp2=x2
    temp3=x3

    if temp1 > temp2:
        temp1, temp2 = temp2, temp1
    if temp2 > temp3:
        temp2, temp3 = temp3, temp2
    if temp1 > temp2:
        temp1, temp2 = temp2, temp1

    if order is None:
        return [temp1, temp2, temp3]
    else:
        return [temp3, temp2, temp1]

def test2(x1=8,x2=5,x3=10,order=None):
    ''' '''
    temp=[x1,x2,x3]

    if order is None:
        temp.sort()
    else:
        temp.reverse()

    return temp

#print test1(3,5,8)
#print test2(3,5,8)

if __name__ == "__main__":
    from timeit import Timer
    t2 = Timer("test2(30,5,10)", "from __main__ import test2")
    print "test2:",t2.timeit(100000)
    t1 = Timer("test1(30,5,10)", "from __main__ import test1")
    print "test1:",t1.timeit(100000)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值