我最近使用timeit模块对python进行了非常简单的性能测试.结果真的让我目瞪口呆:由所消耗的时间x=x+x
大约是125倍的x+x
还是y=x+x,
为什么?我真的希望有人会给我一些关于此的线索,也许我使用了timeit错误?谢谢!
请注意,y=x+x;x=y
它和x=x+x
...... 一样慢但是x=x+47
速度一样快x+x
testBasicOps()
testcase ="pass",时间流逝:0.001487secs
testcase ="x = 47",时间流逝:0.002424秒
testcase ="x = 94",时间流逝:0.002423秒
testcase ="x = 47*2",时间流逝:0.002423秒
testcase ="x + x",时间流逝:0.003922秒
testcase ="x*2",时间流逝:0.005307secs
testcase ="x = x + x",时间流逝:0.497974secs
testcase ="x = x*2",时间流逝:0.727506secs
testcase ="x = x + 47",时间流逝:0.005770秒
testcase ="x = 47 + x",时间流逝:0.004442秒
testcase ="x + = x",时间流逝:0.498920secs
testcase ="y = x + x",时间流逝:0.004102secs
testcase ="y = x*2",时间流逝:0.006327secs
测试用例="Y = X + …
INFILE:
[start] cmd1
afadfadf
dafdf
[ok] cmd1
[-] cmd2
[-] cmd3
[start] cmd4
dfdafadf
d
afasdf
daf
[stop] cmd4
[-] cmd5
[-] cmd6
[start] cmd1
adfadd
dafa
dfdd33r55ae
[ok] cmd1
[-] cmd7
[start] cmd8
error...
[stop] cmd8
[-] cmd9
[start] cmd10
exit xx
[stop] cmd10
[-] cmd
[start] cmd1
[ok] cmd1
Run Code Online (Sandbox Code Playgroud)
我想打印所有块,如:[开始] ... [停止] cmd ...
结果应该是:
[start] cmd4
dfdafadf
d
afasdf
daf
[stop] cmd4
[start] cmd8
error...
[stop] cmd8
[start] cmd10
exit xx
[stop] cmd10
Run Code Online (Sandbox Code Playgroud)
我怎么能用sed做到这一点?
sed -n '/\[start\]/I,/\[stop\]/I …
无论输入值是什么,np.genfromtxt
都将始终返回False
。
使用dtype='u1'
我按预期得到'1'。但是使用dtype='b1'
(Numpy's bool)我得到'假'。
它%TS
总是输出小数部分。有没有办法去掉小数部分?
find ./ -type f -printf "%TY%Tm%Td-%TH%TM%TS %P\n"
20200813-133459.6920994010 export.txt
Run Code Online (Sandbox Code Playgroud) 我在C / C ++中找到了一些实现,例如voronoi framework。通常这些代码需要密集的循环,这在python中是不好的。有没有可以在python中调用的内置骨架函数?
是否可以导入 std::find,在那里libcpp.algorithm
我只找到非常有限的功能。现在我必须循环遍历向量并进行比较。
说我有函数指针R(*fp)(...A)
。有什么方法可以获取类型R
和类型列表A...
吗?std::invoke_result_t
似乎不起作用。而且我找不到参数列表的任何内容。
#include <iostream>
#include <cstdlib>
#include <type_traits>
typedef int(*fp_t)(int, float);
std::invoke_result_t<fp_t> x = 10;
int main()
{
std::cout << "Hello, Wandbox!" << std::endl;
std::cout << x << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
用c ++ 17编译会出现错误:
/opt/wandbox/gcc-head/include/c++/10.0.0/type_traits: In substitution of 'template<class _Fn, class ... _Args> using invoke_result_t = typename std::invoke_result::type [with _Fn = int (*)(int, float); _Args = {}]':
prog.cc:6:26: required from here
/opt/wandbox/gcc-head/include/c++/10.0.0/type_traits:2917:11: error: no type named 'type' in 'struct std::invoke_result<int (*)(int, float)>'
2917 | …
Run Code Online (Sandbox Code Playgroud) c++ function-pointers variadic-templates template-argument-deduction c++17
numpy.trunc
是一个基于abs值的floor函数:
a = np.array([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0])
np.floor(a)
Out[122]: array([-2., -2., -1., 0., 1., 1., 2.])
np.trunc(a)
Out[123]: array([-1., -1., -0., 0., 1., 1., 2.])
Run Code Online (Sandbox Code Playgroud)
ceil输出是这样的:
np.ceil(a)
Out[124]: array([-1., -1., -0., 1., 2., 2., 2.])
Run Code Online (Sandbox Code Playgroud)
但我想要一个输出:array([ - 2., - 2.,-1.,1.,2.,2.,2.])这个numpy函数是什么?
编辑:不幸的是,零函数没有内置圆.基于@Mitch和@Divakar的答案,我做了一些测试,并尝试优化速度和内存使用.
def rawzero1(a): #@Mitch
return np.sign(a)*np.ceil(np.abs(a))
def rawzero2(a): #@Divakar
return np.where(a<0, np.floor(a), np.ceil(a))
def rawzero3(a):
_a = np.abs(a)
np.ceil(_a, _a) # inplace
np.copysign(_a, a, out = _a)
return _a
def rawzero4(a):
_a = np.ceil(np.abs(a))
np.copysign(_a, a, …
Run Code Online (Sandbox Code Playgroud)