我有一个程序调用我制作的 shell 脚本工具,该工具通过目录压缩文件并获取校验和值并调用其他一些工具来上传文件。手术大约需要 3 到 4 分钟。
我这样称呼脚本:
int result = system("/bin/sh /path/to/my/script");
Run Code Online (Sandbox Code Playgroud)
通过使用exec()函数系列,我也得到了相同的结果:
int child = fork();
if(child == 0) {
execl( "/bin/sh", "sh", "/path/to/my/script", (char*)0 );
}
Run Code Online (Sandbox Code Playgroud)
我知道有exec你可以输出重定向到父程序,以便它可以读取的命令行工具的输出,但除此之外,什么时候应该使用system,而不是exec?
我正在用C++编写程序,我正在讨论是否将"if"放在循环中.我会想象做一次检查,然后循环总体上更有效,而不是常量循环和检查,但我不太确定.或者这一点无关紧要,因为编译器无论如何都会优化它?
这更有效吗?
for(int i = 0; i < SOME_BOUND; i++){
if(SOME_CONDITION){
//Some actions
}
else {
//Some actions
}
}
Run Code Online (Sandbox Code Playgroud)
或者这更有效率?
if(SOME_CONDITION){
for(int i = 0; i < SOME_BOUND; i++){
//Some Actions
}
}
else {
for(int i = 0; i < SOME_BOUND; i++){
//Some Actions
}
}
Run Code Online (Sandbox Code Playgroud) 我在C++中有这个父类
//ParentClass header file
public ParentClass{
public:
ParentClass();
virtual void someParentFunction();
private:
//other member variables and functions
};
//Functions implemented done in respective .cpp file
Run Code Online (Sandbox Code Playgroud)
我扩展了这个课程,所以我有一个看起来像这样的孩子
//ChildOneClass header file
public ChildOneClass : public ParentClass{
public:
//Constructors and other functions
private:
//Other members
};
//Functions implemented in respective .cpp file
Run Code Online (Sandbox Code Playgroud)
示例声明:
//Dynamically create one ChildOneClass object
ChildOneClass * c = new ChildOneClass();
//I know this is never done, but for example purposes i just did this
void * v = c; …Run Code Online (Sandbox Code Playgroud)