我尝试编写此代码来计算Ackerman值以及该函数被调用的次数。但是,计数器始终保持为0。你能帮我吗?
/*
A(m,n) = n+1, if m==0
A(m,n) = A(m-1,1), if m>0 and n==0
A(m,n) = A(m-1,A(m,n-1)), if m>0 and n>0
*/
#include<stdio.h>
static int w=0;
int ackerman(int m,int n)
{
w=w+1;
if(m==0)
return n+1;
else if(m>0 && n==0)
return ackerman(m-1,1);
else if(m>0 && n>0)
return ackerman(m-1,ackerman(m,n-1));
}
int mainackerman()
{
int m,n;
scanf("%d %d",&m,&n);
printf("%d %d",ackerman(m,n),w);
return 0;
}
Run Code Online (Sandbox Code Playgroud) do_something()我必须检查一对包含 30k 元素的列表总共花费了多少时间。下面是我的代码
def run(a, b, data):
p = datetime.datetime.now()
val = do_something(a, b, data[0], data[1])
q = datetime.datetime.now()
res = (q - p).microseconds
return res
Run Code Online (Sandbox Code Playgroud)
接下来,我使用以下代码调用它:
func = functools.partial(run, a, b)
x = np.linspace(500, 1000, 30000).tolist()
y = np.linspace(20, 500, 30000).tolist()
data = zip(x, y)
with multiprocessing.Pool(processes=multiprocessing.cpu_count()) as pool:
d = pool.map(func, data)
res = sum(d)
Run Code Online (Sandbox Code Playgroud)
每当我运行这个时,我都会不断得到OSError: [Errno 24] Too many open files. 我该如何解决?