- ·上一篇文章:C#基础学习 —— 异步编程篇
- ·下一篇文章:常用的CSS命名规则(web标准化设计)
用python测试代码的运行时间
原题:
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
测试代码:
from time import clock as now
def getNatureSum(beg , max):
n = max / beg
end = beg * n
return (beg + end) * n / 2
def getSum(max) :
print getNatureSum( 3 , max ) + getNatureSum( 5 , max) - getNatureSum(15 , max)
start = now()
getSum(999)
finish = now()
print (finish-start)
改进后的代码:
from time import clock as now
start = now()
a = range(3 , 1000 , 3)
b = range(5 , 1000 , 5)
c = range(15 , 1000 , 15)
print sum(a) + sum(b) - sum(c)
finish = now()
print (finish-start)
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
测试代码:
from time import clock as now
def getNatureSum(beg , max):
n = max / beg
end = beg * n
return (beg + end) * n / 2
def getSum(max) :
print getNatureSum( 3 , max ) + getNatureSum( 5 , max) - getNatureSum(15 , max)
start = now()
getSum(999)
finish = now()
print (finish-start)
改进后的代码:
from time import clock as now
start = now()
a = range(3 , 1000 , 3)
b = range(5 , 1000 , 5)
c = range(15 , 1000 , 15)
print sum(a) + sum(b) - sum(c)
finish = now()
print (finish-start)


