作为 Julia 的新手,我和其他许多人一样,对 Julia 中的循环创建自己的本地作用域(但不在 REPL 上或在函数内)这一事实感到困惑。网上有很多关于这个主题的讨论,但这里的大多数问题都是关于这种行为的细节,例如为什么在循环内执行 a=1 不会影响循环外的变量 a,但 a[1]=1作品。我现在大部分情况都知道它是如何工作的了。
我的问题是为什么 Julia 会采用这种行为。从用户的角度来看,这有好处吗?我想不出一个。或者出于某种技术原因有必要吗?
如果已经有人问过这个问题,我深表歉意,但到目前为止我看到的所有问题和答案都是关于它是如何工作的以及如何处理它,但我很好奇为什么 Julia 是这样实现的。
我用这个标题的意思是,在某些情况下,在构建整个程序之后,它的第一次执行将需要大约 25 秒才能开始(直到第一个 printf 显示在控制台上)。下一次执行几乎立即开始(应该如此)。添加/删除一个空格并再次编译,之后的第一次执行再次极其缓慢。
Weather 我从 IDE (Code::Blocks) 或文件资源管理器中运行它没有任何改变。
但这是“解决”问题的方法:
我写的程序有一个循环,它一直在等待用户输入:
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
printf("Welcome!\n");
bool Running=true;
do{
char input[256], command[64];
if(fgets(input, 256, stdin) == NULL || input[0]=='\n')
continue;
sscanf(input, "%s", command);
string command_cppstr(command);
if(command_cppstr == "help")
{
printf("\n");
printf("help - displays this list\n");
printf("exit / quit - exits this progam\n\n");
continue;
}
if(command_cppstr == "exit" || command_cppstr == "quit")
{
Running = false;
continue;
}
printf("Unrecognized command. Use command \"help\" for …Run Code Online (Sandbox Code Playgroud)