for x in 100:
#what to do every time (100 times): replace this line with your every-iteration functions.
if x % 5 == 0:
#what to do every 5th time: replace this line with your nth-iteration functions.
B区,概括
n = 5
for x in 100:
#what to do every time (100 times): replace this line with your every-iteration functions.
if x % n == 0:
#what to do every 5th time: replace this line with your nth-iteration functions.
printcounter = 0
# Whatever a while loop is in Python
while (...):
...
if (printcounter == 1000000):
print('Progress report...')
printcounter = 0
...
printcounter += 1
喂,老兄?如果需要其他评论/解释,请联系我:
一。
x
和n
的人类语言声明:设x为在给定时间检查的迭代次数。 设n为代码将在其上执行的迭代的倍数。
2。我们要做的是:
第一个代码块(块A)仅使用一个变量x(定义见上文),并使用5(整数)而不是变量n(定义见上文)。
第二个代码块(块B)同时使用上面定义的变量(x和n)。整数5将被变量n替换,因此,块B在第n次迭代时字面上执行操作。
我们的目标是每第X次迭代和每第5次/第N次迭代都做一些事情。 我们将经历100次迭代。
m、 易于理解的代码:
A区,最小变量:
B区,概括
请告诉我,如果你有任何问题,因为我没有时间测试后,写在这里。
三。练习
关于模和其他基本运算符: https://docs.python.org/2/library/stdtypes.htmlhttp://www.tutorialspoint.com/python/python_basic_operators.htm
关于海龟: https://docs.python.org/2/library/turtle.htmlhttps://michael0x2a.com/blog/turtle-examples
当你到达想要的号码时,保留一个计数器并将其重置为零怎么样?添加和检查相等性比模快。
尽管编译器很有可能已经在为您进行类似的优化。。。但这可以让你安心。
真的减速了吗?你得亲自去看看。不会有太大的减速,但如果我们讨论的是纳秒,那可能是相当可观的。或者,可以将一个1000万循环转换为两个较小的循环:
相关问题 更多 >
编程相关推荐