什么方法可以使python计数最快?

什么方法可以使python计数最快?,python,Python,我正在上化学课,讲的是这个数字有多大。我看到一段文字说: 一台每秒能计算10000000个原子的计算机需要 20亿年来计算1摩尔物质 我认为向全班同学展示这一点会很酷。所以我有剧本: import time t_end = time.time() + 1 i=0 while time.time() < t_end: i += 1 print(i) 导入时间 t_end=time.time()+1 i=0 当time.time()结束时: i+=1 印刷品(一) 这将打印结

我正在上化学课,讲的是这个数字有多大。我看到一段文字说:

一台每秒能计算10000000个原子的计算机需要 20亿年来计算1摩尔物质

我认为向全班同学展示这一点会很酷。所以我有剧本:

import time

t_end = time.time() + 1
i=0

while time.time() < t_end:
    i += 1

print(i)
导入时间
t_end=time.time()+1
i=0
当time.time()结束时:
i+=1
印刷品(一)

这将打印结果:
6225324
。这大约是正确的数量级,但绝对低于该语句。写这篇文章的更有效的方法是什么?

由于
time.time()
轮询,代码中的开销太大。这是一个系统调用,它不是免费的,因此它使您的计数器相形见绌,并使您的度量值产生偏差

在python中,我能想到的最好方法是:

import time

start_time = time.time()
for i in range(1,10000000):  # use xrange if you run python 2 or you'll have problems!!
    pass
print("counted 10 million in {} seconds".format(time.time()-start_time))
在我的电脑上,这样做需要0.5秒


当然,在解释python时,使用
pypypy
运行python会有更好的结果,或者更好:使用C之类的编译语言(甚至是具有JIT的Java)。

如果您只是想证明Avogadro的数字是一个非常大的数字,您可以这样做:

import time 
avo=602214085700000000000000
sec_per_year=60*60*24*365.25

t0=time.time()
for i in range(avo):    
    if i and i%10000000==0:
        t=time.time()-t0
        avg_per_sec=i/t
        per_year=avg_per_sec*sec_per_year
        print("{:15,} in {:,.2f} sec -- only {:,} more to go! (and {:,.2f} years)".format(i, t, avo-i,avo/per_year))
印刷品:

 10,000,000 in 2.17 sec -- only 602,214,085,699,999,990,000,000 more to go! (and 4,140,556,225.48 years)
 20,000,000 in 4.63 sec -- only 602,214,085,699,999,980,000,000 more to go! (and 4,422,153,353.15 years)
 30,000,000 in 7.12 sec -- only 602,214,085,699,999,970,000,000 more to go! (and 4,530,778,737.84 years)
 40,000,000 in 9.58 sec -- only 602,214,085,699,999,960,000,000 more to go! (and 4,571,379,181.80 years)
 50,000,000 in 12.07 sec -- only 602,214,085,699,999,950,000,000 more to go! (and 4,605,790,562.41 years)
 ...
100,000,000 in 0.93 sec -- only 602,214,085,699,999,900,000,000 more to go! (and 176,883,113.10 years)
200,000,000 in 1.85 sec -- only 602,214,085,699,999,800,000,000 more to go! (and 176,082,858.48 years)
300,000,000 in 2.76 sec -- only 602,214,085,699,999,700,000,000 more to go! (and 175,720,835.29 years)
400,000,000 in 3.68 sec -- only 602,214,085,699,999,600,000,000 more to go! (and 175,355,661.40 years)
500,000,000 in 4.59 sec -- only 602,214,085,699,999,500,000,000 more to go! (and 175,114,044.92 years)
600,000,000 in 5.49 sec -- only 602,214,085,699,999,400,000,000 more to go! (and 174,641,142.93 years)
700,000,000 in 6.44 sec -- only 602,214,085,699,999,300,000,000 more to go! (and 175,612,486.37 years)

对于PyPy或Python2,您需要使用
while
循环,因为xrange溢出了avogadros编号:

from __future__ import print_function

import time 
avo=602214085700000000000000
sec_per_year=60*60*24*365.25

t0=time.time()
i=0
while i<avo:
    i+=1
    if i and i%100000000==0:
        t=time.time()-t0
        avg_per_sec=i/t
        per_year=avg_per_sec*sec_per_year
        print("{:15,} in {:,.2f} sec -- only {:,} more to go! (and {:,.2f} years)".format(i, t, avo-i,avo/per_year))

我不明白这个问题。您是否在寻求一种更有效的方法来使用Python进行增量操作?或者你在问为什么你的代码只执行这么多的迭代/增量?有没有更有效的方法来执行增量可能是更好的措辞。然后答案是“没有”。如果您需要更快的性能,您必须切换到不同的编程语言(例如:C,甚至汇编),尽管我不确定您是否真的会看到这么大的差异。我只有一台笔记本电脑:)您将在6亿年后完成,还不错。它是
pypy
pypi
是python包索引。