我正在尝试2.6的集合理解,并遇到了以下两种方式.我认为第一种方法比第二种方法更快,timeit否则建议.为什么第二种方法更快,即使第二种方法有一个额外的列表实例化后跟一组实例化?
方法1:
In [16]: %timeit set(node[0] for node in pwnodes if node[1].get('pm'))
1000000 loops, best of 3: 568 ns per loop
Run Code Online (Sandbox Code Playgroud)
方法2:
In [17]: %timeit set([node[0] for node in pwnodes if node[1].get('pm')])
1000000 loops, best of 3: 469 ns per loop
Run Code Online (Sandbox Code Playgroud)
哪里pwnodes = [('e1', dict(pm=1, wired=1)), ('e2', dict(pm=1, wired=1))].
我对矢量push_back行为的方式有点困惑,使用下面的代码片段我预计复制构造函数只能被调用两次,但输出建议不然.是矢量内部重组导致这种行为.
输出:
Run Code Online (Sandbox Code Playgroud)Inside default Inside copy with my_int = 0 Inside copy with my_int = 0 Inside copy with my_int = 1
class Myint
{
private:
int my_int;
public:
Myint() : my_int(0)
{
cout << "Inside default " << endl;
}
Myint(const Myint& x) : my_int(x.my_int)
{
cout << "Inside copy with my_int = " << x.my_int << endl;
}
void set(const int &x)
{
my_int = x;
}
}
vector<Myint> myints;
Myint x;
myints.push_back(x);
x.set(1);
myints.push_back(x);
Run Code Online (Sandbox Code Playgroud) 以下比较不起作用,有没有办法从时间模块中获取自纪元以来经过的秒数(wrt localtime)?
(datetime.datetime.now() - datetime.datetime.utcfromtimestamp(0)).total_seconds() - time.mktime(time.localtime()) => 3600.9646549224854
Run Code Online (Sandbox Code Playgroud)
或者
(datetime.datetime.now() - datetime.datetime.utcfromtimestamp(0)).total_seconds() - time.time() => 3599.9999861717224
Run Code Online (Sandbox Code Playgroud)
谢谢