次のプログラム (関数 prime 内) の実行時間は 110.726383227 秒です
関数(プライム)でラップせずに同じプログラムを実行すると、実行時間は222.006502634秒です
関数でラップすることで、パフォーマンスが大幅に向上しました。
このプログラムの効率を高める可能性はまだありますか?
# This is a program to find sum of prime numbers till 2 billion
def prime():
import time
start_time = time.clock()
num = 2
j=1
tot_sum = 0
for num in xrange(2,2000000):
count = 0
for j in xrange(1,int(num**0.5)+1): # checking from 1 to sqrt(n)+1
if(num % j == 0):
count = count+1
if(count == 1):
tot_sum = tot_sum + num
print "total sum is %d" % tot_sum
print time.clock() - start_time, "seconds"