如果像我一样,你在一个While(True)循环的位置颤抖,那么你也必须经过长时间的努力思考重构它的最佳方法.我已经看到了几种不同的实现,没有一种比其他任何实现更好,例如timer和delegate组合.
那么你提出或看到重构可怕的While(True)循环的最佳方法是什么?
编辑:正如一些评论所提到的,我的意图是这个问题是一个"无限循环"重构,例如运行Windows风格的服务,其中唯一的停止条件是OnStop或致命异常.
怎么了?有人可以解释一下这里发生了什么,我在紧密循环中改变了:
## j=i
## while j < ls - 1 and len(wordlist[j]) > lc: j+=1
j = next(j for j in range(i,ls) if len(wordlist[j]) <= lc)
Run Code Online (Sandbox Code Playgroud)
评论的版本运行整个程序:625毫秒,下一个生成器版本在2.125秒的时间内运行整个程序.
有什么理由可以说这个更加pythonic的版本会导致性能上的这种灾难?
编辑:也许它是由使用psyco模块引起的?当然至少没有psyco的Python 2.7的运行时间对于下一个版本来说是2.141,意味着与使用psyco的Python 2.6几乎相同.
删除*.pyc文件后,我得到的代码没有减速.然后,当我从库模块中删除了psyco的导入时,我还得到了2.6时序,没有psyco使用,非psyco版本和psyco版本的结果(现在库例程也慢了,它的时间也相关:)
不是psyco:
Psyco是:
在带有2GB RAM的WindowsXP AMD Sempron 3100+系统中运行.使用两个全局变量计算循环和调用:
j=i
callcount += 1
while j < ls - 1 and len(wordlist[j]) > lc:
j+=1
loopcount += 1
Run Code Online (Sandbox Code Playgroud)
使用psyco进行测试输入的结果:
Finished in 625 ms
Loopcount: 78317 …Run Code Online (Sandbox Code Playgroud) 有两种方法可以逐行读取文件,我想在这里讨论:
#!/bin/bash
while read line
do
echo-e "$ line \ n"
done <file.txt
Run Code Online (Sandbox Code Playgroud)
和
#!/bin/bash
exec 3<file.txt
while read line
do
echo-e "$ line \ n"
done
Run Code Online (Sandbox Code Playgroud)
所以第一个版本工作正常,但我不明白循环文件的工作机制.但我理解的第二个版本的机制.但在这里,我不明白为什么它挂起,不打印任何东西.
var mdflag;
var count = 0;
document.addEventListener("mousedown",mdown,false);
document.addEventListener("mouseup",mup,false);
}
function mdown()
{
mdflag=true;
while(mdflag)
document.getElementById("testdiv").innerHTML = count++;
}
function mup()
{
mdflag = false;
}
Run Code Online (Sandbox Code Playgroud)
我想要在鼠标停止时运行代码,我无法找到任何建议我可以做的同时(mousedown)所以我尝试为mousedown制作一个标志,这是在鼠标上重置然而我相信while循环是什么导致我陷入无限循环.
有什么建议可以帮助我实现的目标吗?
我正在比较Java中嵌套for,while和do-while循环的效率,并且我遇到了一些我需要帮助理解的奇怪结果.
public class Loops {
public static void main(String[] args) {
int L = 100000; // number of iterations per loop
// for loop
double start = System.currentTimeMillis();
long s1 = 0;
for (int i=0; i < L; i++) {
for (int j = 0; j < L; j++) {
s1 += 1;
}
}
double end = System.currentTimeMillis();
String result1 = String.format("for loop: %.5f", (end-start) / 1000);
System.out.println(s1);
System.out.println(result1);
// do-while loop
double start1 = System.currentTimeMillis();
int i = …Run Code Online (Sandbox Code Playgroud) 所以,我最近进入python编程并决定编写一个简单的代码,运行一些简单的数学,例如计算三角形中的缺失角度以及其他类似的简单事物.在我制作了这个程序和其他一些程序后,我想也许我知道的其他人可以使用它,所以我决定尝试尽可能简单.代码可以在下面找到:
a = int(input("What's one of the angles?"))
b = int(input("What's the other angle in the triangle?"))
c = (a + b)
d = 180
f = int(180 - c)
print(f)
Run Code Online (Sandbox Code Playgroud)
代码本身确实有效,但唯一的问题是如果你有一个以上的问题,那么持续加载Python并点击F5就变得单调乏味而且相当繁琐,我的想法是让它循环无数次直到你决定关闭该计划.每当我尝试寻找一种方法来执行此操作时,所有的True语句都是针对更大更复杂的代码片段而这可能是我的第五或第十段代码,我无法理解其中的一些编码.
如果有人愿意提供帮助,我会很感激这个主题的任何帮助或建议.
我最近开始使用K&R书(第2版)开始学习C,而我只是在思考这个解决方案时遇到麻烦,以便练习1-9,这是:
编写一个程序将其输入复制到其输出,用一个空格替换一个或多个空格的每个字符串.
我在网上找到了以下解决方案,除了putchar上面的分号('');它才有意义.没有它,程序不能正常执行其功能,该分号有什么功能?
#include <stdio.h>
int main(void)
{
int c;
while ((c = getchar()) != EOF) {
if(c == ' ') {
while((c = getchar()) == ' ')
;
putchar(' ');
}
putchar(c);
}
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我正在寻找一个函数,它接受一个向量并不断删除第一个值,直到向量的总和小于20.返回剩余的值.
我已经尝试了for-loop和while循环,但无法找到解决方案.
vec <- c(3,5,3,4,3,9,1,8,2,5)
short <- function(vec){
for (i in 1:length(vec)){
while (!is.na((sum(vec)) < 20)){
vec <- vec[i+1:length(vec)]
#vec.remove(i)
}
}
Run Code Online (Sandbox Code Playgroud)
预期输出应为:
1,8,2,5
小于20.
是否可以在 Python 的 while 循环中使用生成器或迭代器?例如,类似于:
i = iter(range(10))
while next(i):
# your code
Run Code Online (Sandbox Code Playgroud)
这样做的目的是将迭代构建到 while 循环语句中,使其类似于 for 循环,不同之处在于您现在可以在 while 语句中添加额外的逻辑:
i = iter(range(10))
while next(i) and {some other logic}:
# your code
Run Code Online (Sandbox Code Playgroud)
然后它成为一个很好的 for 循环/while 循环混合。
有谁知道如何做到这一点?
我永远不明白为什么使用do while循环是必要的.我理解他们做了什么,这是执行while循环包含的代码而不检查条件是否为真.
但不是以下代码:
do{
document.write("ok");
}
while(x == "10");
Run Code Online (Sandbox Code Playgroud)
完全相同:
document.write("ok");
while(x == "10"){
document.write("ok");
}
Run Code Online (Sandbox Code Playgroud)
也许我是非常愚蠢的,并且遗漏了一些显而易见的东西,但是我没有看到在我上面的例子中使用do的好处.
while-loop ×10
loops ×3
python ×3
do-while ×2
for-loop ×2
generator ×2
performance ×2
bash ×1
boolean ×1
c ×1
file-io ×1
iterator ×1
java ×1
javascript ×1
mousedown ×1
optimization ×1
python-3.x ×1
r ×1
refactoring ×1
string ×1
vector ×1