我一直试图使用GNU并行一段时间,但我从来没有能够让它完全运行!
例如,运行(在非空目录中!):
ls | parallel echo # Outputs single new line
ls | parallel echo echo echo # Outputs three new lines.
ls | parallel echo {} # /bin/bash: {}: command not found
ls | parallel echo '{}' # /bin/bash: {}: command not found
ls | parallel 'echo {}' # Outputs: {}
ls | parallel -IMM 'echo MM' # Outputs: MM
Run Code Online (Sandbox Code Playgroud)
它似乎只是将每个参数作为命令执行,这没有任何意义.
我试过bash,zsh,tcsh,csh和sh,但无济于事.
是否可以为目标指定不同的名称或别名,以便可以使用原始目标名称或别名来调用它.
比如像
/very/long/path/my_binary: dep_a dep_b dep_c
# Compile
# Desired command
ALIAS my_binary = /very/long/path/my_binary
# NOTE: Notice the use of 'my_binary' in the dependencies
data1: my_binary datafile
# Build data file using compiled my_binary
Run Code Online (Sandbox Code Playgroud)
我尝试过使用.PHONY
目标:
.PHONY: my_binary
my_binary: /very/long/path/my_binary
Run Code Online (Sandbox Code Playgroud)
从命令行调用时,这很有用:
# Runs rule 'my_binary' and then *only* runs rule '/very/long/path/my_binary'
# if the rule '/very/long/path/my_binary' needs updating.
make my_binary
Run Code Online (Sandbox Code Playgroud)
但是,当别名my_binary
列为依赖项时,这不起作用:
# *Always* thinks that rule 'data1' needs updating, because it always thinks that …
Run Code Online (Sandbox Code Playgroud) Python中切片操作的迭代效率如何?如果切片不可避免地复制,是否有替代方案?
我知道列表上的切片操作是O(k),其中k是切片的大小.
x[5 : 5+k] # O(k) copy operation
Run Code Online (Sandbox Code Playgroud)
然而,当迭代列表的一部分时,我发现最干净(和大多数Pythonic?)的方式(不必求助于索引)是这样做的:
for elem in x[5 : 5+k]:
print elem
Run Code Online (Sandbox Code Playgroud)
然而,我的直觉是,这仍然会导致子列表的昂贵副本,而不是简单地迭代现有列表.
请考虑以下代码,该代码使用带有可变参数的函数:
#include <iostream>
// Typedef function type
template<typename... Output>
using Func = void(Output*...);
// Function runner
template<typename... Output>
void run_func(Func<Output...>& func, Output*... output) {
for (int i=0 ; i < 10 ; ++i) {
func(output...);
}
}
void f(double* d) {
*d *= 2;
};
int main() {
double value = 1.0;
run_func(f, &value);
printf("%f\n", value);
}
Run Code Online (Sandbox Code Playgroud)
用g ++ 4.7.3编译它可以正常工作,并且运行1024.0
按预期生成.
使用icpc 14.0.2进行编译会使其崩溃...
templ.cc(21): internal error: assertion failed: lower_expr: bad kind (shared/cfe/edgcpfe/lower_il.c, line 18582)
run_func(f, &value);
^
Run Code Online (Sandbox Code Playgroud)
使用clang 3.5.0-1进行编译会出现以下错误消息: …
我有一个包含unique_ptr
相互依赖的字段的类:
class ResourceManager {
ResourceManager() {}
ResourceManager(A* a_ptr) :
b_ptr(new B(a)),
c_ptr(new C(b_ptr.get())) {}
ResourceManager& operator=(ResourceManager&& that) {
// Call destructor, then construct a new instance on top
~ResourceManager();
ResourceManager* new_this = new(this) ResourceManager();
// Surely this must be the case, right?
// Is there any reason to prefer using either?
assert(new_this == this);
new_this->b_ptr = that.b_ptr;
new_this->c_ptr = that.c_ptr;
return *new_this;
}
unique_ptr<B> b;
unique_ptr<C> c;
};
Run Code Online (Sandbox Code Playgroud)
这里的用例是我想将新值重新分配给指针,同时保持ResourceManager
作为堆栈分配的变量,或者作为非指针类成员.
使用我当前的设置,我想像使用这样的东西:
A a, another_a;
ResourceManager …
Run Code Online (Sandbox Code Playgroud) GregorianCalendar的最大值和最小值是多少?
它们是一个常数,如Integer.MAX_VALUE,还是GregorianCalendar.get(BLAH)?
简而言之,如何创建具有最小/最大值的GregorianCalendar实例?
虽然我知道这可能不是最好的想法,但我假设:
(即定义行为)手动调用对象的析构函数,然后将内存重用于另一个对象是否合法?
定义:
class A {
int a, b, c;
A() {}
~A() {}
}
A createObject() {
A object;
return object;
}
Run Code Online (Sandbox Code Playgroud)
码:
A* object = new A();
// Use object...
object->~A();
*object = createObject();
Run Code Online (Sandbox Code Playgroud) 考虑这个简单的类:
struct C {
C(const char* name) : name(name) {
cout << "constructing " << name << endl;
}
~C() {
cout << "destructing " << name << endl;
}
string name;
};
Run Code Online (Sandbox Code Playgroud)
我想有一个指向这个类的实例的指针,它通常被另一个实例替换.但是,我希望在创建新实例之前销毁当前实例.
如果我unique_ptr
以正常方式使用a ,这不起作用:
unique_ptr<C> c( new C("the first one"));
c.reset(new C("the second one"));
Run Code Online (Sandbox Code Playgroud)
(不受欢迎的)输出:
构建第一个
构建第二个
破坏第一个
破坏第二个
可以如下实现期望的效果:
unique_ptr<C> c( new C("the first one"));
c.reset(); // explicitly destruct the first one first
c.reset(new C("the second one"));
Run Code Online (Sandbox Code Playgroud)
输出: …
c++ smart-pointers resource-management resourcemanager unique-ptr
c++ ×4
alias ×1
c++11 ×1
calendar ×1
clang ×1
destructor ×1
gnu ×1
gnu-parallel ×1
iteration ×1
java ×1
makefile ×1
performance ×1
python ×1
raii ×1
slice ×1
synonym ×1
target ×1
templates ×1
unique-ptr ×1
xargs ×1