标签: while-loop

while(true) 无限循环是否钉住 CPU?

我有一种情况,我需要在服务中运行一个恒定循环,每次检查某些条件,然后根据需要采取行动。从设计的角度来看,使用while (true)循环非常适合:

while (true)
{
    Process();
}
Run Code Online (Sandbox Code Playgroud)

Process() 方法检查内存中(快速)数据存储的状态。如果它发现基于状态有工作要做,它会使用它处理它Task.Run()(因此控制立即返回到循环)。

我已经看到一些参考资料,这会消耗不必要的 CPU 周期,我应该使用Timer, 或添加Thread.Sleep到循环中。我还看到帖子说使用while (true).

在我的开发机器上,虽然 CPU 使用率确实上升了,但它似乎没有负面影响。可能是 CPU 使用率分布在多个核心上,而部署环境可能只有一个核心。

while (true)循环是否对 CPU 产生负面性能影响,如果是,减轻影响的正确方法是什么?

c# infinite-loop while-loop

3
推荐指数
1
解决办法
3193
查看次数

如何在 Python 中运行后台计时器

我目前正在制作一个数学游戏,用户有 60 秒的时间来回答尽可能多的问题。到目前为止,除了计时器应该倒计时到 0 或计数到 60 然后停止游戏之外,我一切正常。现在,我将计时器设置为 time.clock() 以计数到 60,当计时器小于该值时,游戏将继续运行。然而,出于某种原因, time.clock() 并没有像我期望的那样工作。我还尝试同时运行两个 while 循环,但也没有用。任何人都可以帮助我吗?只是寻找一种在后台运行计时器的方法。

这是我的代码:

    score = 0
    timer = time.clock()
    lives = 3

    while timer < 60 and lives > 0:
        if score >= 25:
            x = random.randint(-100,100)
            y = random.randint(-100,100)
            answer = int(raw_input("What is %d + %d? " % (x,y)))
            if answer == x + y:
                print "Correct!"
                score += 1
            else:
                print "Wrong!"
                lives -= 1
        elif score >= 20:
            x = random.randint(-75,75)
            y = random.randint(-75,75)
            answer …
Run Code Online (Sandbox Code Playgroud)

python time timer while-loop

3
推荐指数
1
解决办法
2万
查看次数

while 循环是否有两个参数?

我的女士给了我一个问题要解决。预测以下代码的输出。

#include <stdio.h>
int main()
{
    int i = 0, j = 0;
    printf("Output is : ");
    while (i < 5, j < 10)    // Doubt: how does while accept 2 arguments?? and how it works??
    {
        i++;
        j++;
    }
    printf("%d, %d\n", i, j);
}
Run Code Online (Sandbox Code Playgroud)

我以为是语法错误。但是当我尝试运行时,它给了我输出。

Output is : 10, 10
Run Code Online (Sandbox Code Playgroud)

但是如何?谁能解释一下?

但是如果我删除第一个 printf 语句printf("Output is : ");并运行它,我的防病毒软件会给我一个Trojan检测到a 的警报。但是它怎么变成了Trojan?

c c++ g++ while-loop

3
推荐指数
1
解决办法
5966
查看次数

使用tail -n读取行时如何使用

问题:我有一个 CSV 转储文件 - 超过 250,000 行。当我使用时while read- 需要一段时间(没有双关语)。我想回到最后 10,000 行来做我需要做的事情,而不是 250,000 行。

代码片段:我当前的代码是这样的:

IFS=","
while read line
do

    awk_var=`echo "$line" | awk -F" " '{print $0}'`

    var_array=($awk_var)

    read -a var_array <<< "${awk_var}"

    echo "${var_array[1]}"


done </some_directory/directory/file_in_question.csv
Run Code Online (Sandbox Code Playgroud)

问题:在阅读with bash 脚本时如何使用tail -n10000with ?while read linefile_in_question.csv

csv bash tail while-loop

3
推荐指数
1
解决办法
8035
查看次数

为什么不推荐在 Scala 中使用 while 循环

scala 样式检查器表示,如果您使用严格的函数样式,则不推荐使用 while 循环- http://www.scalastyle.org/rules-dev.html#org_scalastyle_scalariform_WhileChecker。我找到了 1 个解决方案 -在 Scala 中避免 while 循环有什么好处吗? 这表示可变性将确保从长远来看,您将引入带有 while 模式的错误。这怎么会发生?为什么不检查for 循环如果不变性受到高度限制?我有一个简单的用例,我必须从末尾出现的字符串中删除所有出现的子字符串。我可以找到解决方案,因为我使用了循环。示例 - 字符串是“IABCFGHUABCABC”,子字符串是“ABC”。字符串输出应为“IABCFGHU”,其中删除所有尾随出现的子字符串。是否有任何非强制性和推荐的方法来使用 Scala 解决这个问题?

functional-programming scala while-loop

3
推荐指数
1
解决办法
889
查看次数

反转 R 中的数字

我在 R 中编写了一个代码来反转一个数字。但我得到了inf作为输出。

digit<-512
rev_num<-0
while(digit>0){
  rev_num=rev_num*10 + digit %% 10
 digit=digit / 10
}
print(paste(rev_num))
Run Code Online (Sandbox Code Playgroud)

谁能告诉我这段代码中的错误?

r while-loop

3
推荐指数
2
解决办法
295
查看次数

在python Hetland的书示例中了解区间函数及其参数

从 Hetland 的 python 书中得到了这个示例函数。

def interval(start, stop=None, step=1):
    if stop is None:
        start, stop=0, start
    result = []

    i = start
    while i < stop:
        result.append(i)
        i += step
    return result
Run Code Online (Sandbox Code Playgroud)

两个开始在 if 语句中做什么?为什么会出现两次?我将后者的开始更改为 int,这将我的列表长度更改为该 int。

我似乎也无法围绕交互部分。在这个例子中,start = 10。所以,当迭代...而 10 < 0 它将继续增长列表,每次增加 step=1 的计数。但是,10 不小于 0。首先怎么跑?

python iteration parameters loops while-loop

3
推荐指数
1
解决办法
149
查看次数

C 程序在给出正确的输出后不会结束

所以我正在尝试做一个程序来读取由空格和新行分隔的数字序列。输出应该是相同的序列,但删除不必要的零(字符序列 'EOF' 结束程序)。每个例子

  1. 01492 102934 应该是 1492 102934
  2. 9312 0 01923 应该是 9312 0 1923
  3. 0001249 0000 应该是 1249 0

好吧,我已经实现了这个目的,但遇到了一个障碍。除非我输入 EOF 序列,否则程序不会退出。也许是因为我有一段时间(1)在运行,导致无限循环。但是当我尝试删除它时,程序甚至根本不打印。我还在学习这是一个学校项目。任何帮助将不胜感激!

这是代码:

#include <stdio.h>
int main(){
char c;
int i=0;
while(1){
    c=getchar();
    if (i==0){
        if(c=='0'){
            while (c=='0'){
            c=getchar();
            }
        }
        printf("%c",c);
        i=i+1;
    }
    else if (c==' '){
        printf("%c",c);
        c=getchar();
        if(c=='0'){
            while (c=='0'){
            c=getchar();
            }
        }
        printf("%c",c);
    }
    else if (c=='E'){
        c=getchar();
        if (c=='O'){
            c=getchar();
            if(c=='F'){
                printf("\n");
                return 0;
            }
        }
    }
    else{
        printf("%c",c);
    }
}
Run Code Online (Sandbox Code Playgroud)

}

c while-loop

3
推荐指数
1
解决办法
168
查看次数

用另一个文件的每一行填充文本文件中的空白行

所以基本上我有一个包含很多空行的文本文件。我们将其命名为 time.txt,该文件的一部分如下所示:

       1          5   20    
       2          5   12    
       1          6   3    
       2          6   4

       1          10   30    
       2          10   21    
       1          11   27    
       2          12   8
       1          11   34    
       2          12   20

       1          10   30    
       2          10   21  
Run Code Online (Sandbox Code Playgroud)

现在,我有另一个名为location.txt 的文件,它包含的行数与time.txt 中的空行数相同。它看起来像这样:

   110      -7      5.000              66
   110      -7      5.000              99
   110      -7      5.000              60
Run Code Online (Sandbox Code Playgroud)

而我要的其实很简单:我只是想填补空白行TIME.TXT,每行location.txt,给预期的结果:

   110      -7      5.000              66
       1          5   20    
       2          5   12    
       1          6   3    
       2 …
Run Code Online (Sandbox Code Playgroud)

bash awk while-loop

3
推荐指数
1
解决办法
116
查看次数

如何从 bash 中的文件循环打印 2 列

我有以下内容,output.txt它只包含 2 列来演示:

Test1 Test1-IS-OK
Test2 Test2-IS-NOT
Test3 Test3-IS-OK
Test4 Test4-IS-OK
Test5 Test5-IS-NOT
Run Code Online (Sandbox Code Playgroud)

然后我的 bash 脚本有以下代码:

#!/bin/bash
output="output.txt"
a=$(awk '{ print $1 }' $output)
b=$(awk '{ print $2 }' $output)

while IFS=" " read -r $a $b
do
    echo "LOG: $a and $b"
done < "$output"
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

./test.sh: line 13: read: `Test1-IS-OK': not a valid identifier
Run Code Online (Sandbox Code Playgroud)

我需要有这样的输出

LOG: Test1 and Test1-IS-OK
LOG: Test2 and Test2-IS-NOT
LOG: Test3 and Test3-IS-OK
LOG: Test4 and Test4-IS-OK
LOG: Test5 and Test5-IS-NOT
Run Code Online (Sandbox Code Playgroud)

但是代码不起作用。从文件中循环这 …

bash awk loops while-loop do-while

3
推荐指数
1
解决办法
75
查看次数

标签 统计

while-loop ×10

bash ×3

awk ×2

c ×2

loops ×2

python ×2

c# ×1

c++ ×1

csv ×1

do-while ×1

functional-programming ×1

g++ ×1

infinite-loop ×1

iteration ×1

parameters ×1

r ×1

scala ×1

tail ×1

time ×1

timer ×1