AC功能的错误ouptut返回了python的双重调用

发布于 2025-02-02 16:42:15 字数 2227 浏览 4 评论 0原文

我想加快python代码我的调用交流函数:

我在vanilla python中具有一个函数sum_and_multiply.py.py

def sam_py(lim_sup):
  total = 0
  for i in range(0,lim_sup): # xrange is slower according 
    for j in range(1, lim_sup):            #to my test but more memory-friendly.
      total += (i / j)
  return total

然后我在c sum_and_multiply_c.c.c中具有等价函数:

#include <stdio.h>


double sam_c(int lim_sup){
  int i;
  int j;
  double total;
  total = 0;
  double div;
  for (i=0; i<lim_sup; i++){
    for (j=1; j<lim_sup; j++){

      div = (double) i / j;
//      printf("div: %.2f\n", div);
      total += div;
 //     printf("total: %.2f\n", total);
    }
  }
  printf("total: %.2f\n", total);
  return total;
}

一个文件script.py调用2个函数

from sum_and_multiply import sam_py
import time

lim_sup = 6000

start = time.time()
print(sam_py(lim_sup))
end = time.time()
time_elapsed01 = end - start
print("time elapsed: %.4fs" % time_elapsed01)

from ctypes import *
my_c_fun = CDLL("sum_and_multiply_c.so")
start = time.time()
print(my_c_fun.sam_c(lim_sup))
end = time.time()
time_elapsed02 = end - start
print("time elapsed: %.4fs" % time_elapsed02)
print("Speedup coefficient: %.2fx" % (time_elapsed01/time_elapsed02))

,最后是shell脚本bashscript.zsh comment comment code code code> code> script.py

cc -fPIC -shared -o sum_and_multiply_c.so sum_and_multiply_c.c
python script.py

这是输出:

166951817.45311993
time elapsed: 2.3095s
total: 166951817.45
20
time elapsed: 0.3016s
Speedup coefficient: 7.66x

这是我的问题,尽管C函数正确地计算了结果(作为输出166951817.45通过printf)将输出传递给Python时的输出为20,这是错误的。我怎么能改用166951817.45?

编辑在更改script.py.py.py的最后一部分之后,问题仍然存在:

from ctypes import *
my_c_fun = CDLL("sum_and_multiply_c.so")
my_c_fun.restype = c_double
my_c_fun.argtypes = [ c_int ]
start = time.time()
print(my_c_fun.sam_c(lim_sup))
end = time.time()
time_elapsed02 = end - start
print("time elapsed: %.4fs" % time_elapsed02)
print("Speedup coefficient: %.2fx" % (time_elapsed01/time_elapsed02))

I want to speed up a python code my calling a c function:

I have a the function in vanilla python sum_and_multiply.py:

def sam_py(lim_sup):
  total = 0
  for i in range(0,lim_sup): # xrange is slower according 
    for j in range(1, lim_sup):            #to my test but more memory-friendly.
      total += (i / j)
  return total

then I have the equivalent function in C sum_and_multiply_c.c:

#include <stdio.h>


double sam_c(int lim_sup){
  int i;
  int j;
  double total;
  total = 0;
  double div;
  for (i=0; i<lim_sup; i++){
    for (j=1; j<lim_sup; j++){

      div = (double) i / j;
//      printf("div: %.2f\n", div);
      total += div;
 //     printf("total: %.2f\n", total);
    }
  }
  printf("total: %.2f\n", total);
  return total;
}

a file script.py which calls the 2 functions

from sum_and_multiply import sam_py
import time

lim_sup = 6000

start = time.time()
print(sam_py(lim_sup))
end = time.time()
time_elapsed01 = end - start
print("time elapsed: %.4fs" % time_elapsed01)

from ctypes import *
my_c_fun = CDLL("sum_and_multiply_c.so")
start = time.time()
print(my_c_fun.sam_c(lim_sup))
end = time.time()
time_elapsed02 = end - start
print("time elapsed: %.4fs" % time_elapsed02)
print("Speedup coefficient: %.2fx" % (time_elapsed01/time_elapsed02))

and finally a shell script bashscript.zsh which compile the C code and then call script.py

cc -fPIC -shared -o sum_and_multiply_c.so sum_and_multiply_c.c
python script.py

Here is the output:

166951817.45311993
time elapsed: 2.3095s
total: 166951817.45
20
time elapsed: 0.3016s
Speedup coefficient: 7.66x

Here is my question although the c function calculate correctly the result (as output 166951817.45 via printf) its output when passed to python is 20 which wrong. How could I have 166951817.45 instead?

Edit the problem persists after changing the last part of the script.py as follows:

from ctypes import *
my_c_fun = CDLL("sum_and_multiply_c.so")
my_c_fun.restype = c_double
my_c_fun.argtypes = [ c_int ]
start = time.time()
print(my_c_fun.sam_c(lim_sup))
end = time.time()
time_elapsed02 = end - start
print("time elapsed: %.4fs" % time_elapsed02)
print("Speedup coefficient: %.2fx" % (time_elapsed01/time_elapsed02))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2025-02-09 16:42:15

您假设Python可以“看到”您的功能返回double。但是不能。 C不会“编码”任何内容中的返回类型,因此从库中调用函数的人需要知道其返回类型,或者冒着误解它的风险。

在使用之前,您应该已经阅读cdll的文档!如果您说这是为了锻炼,那么该练习需要包括阅读文档(这是好程序员所做的,没有借口)。

  class ctypes.cdll(name,mode = default_mode,handle = none,use_errno = false,use_last_error = false)
 

此类的实例表示已加载的共享库。这些库中的功能使用标准C调用约定,并被假定返回int

(强调矿山。)

https://docs.python.org/2.7/library/ctypepes.html#return-tml#return-tml#return-types 页面的顶部会告诉您Python2已死,即使您坚持使用它,您也不应该使用它。

my_c_fun = CDLL("sum_and_multiply_c.so")
sam_c = my_c_fun.sam_c
sam_c.restype = c_double
sam_c.argtypes = [ c_int ]
value = sam_c(6000)
print(value)

是要走的路。

You're assuming Python can "see" your function returns a double. But it can't. C doesn't "encode" the return type in anything, so whoever calls a function from a library needs to know its return type, or risk misinterpreting it.

You should have read the documentation of CDLL before using it! If you say this is for the sake of exercise, then that exercise needs to include reading the documentation (that's what good programmers do, no excuses).

class ctypes.CDLL(name, mode=DEFAULT_MODE, handle=None, use_errno=False, use_last_error=False)

Instances of this class represent loaded shared libraries. Functions in these libraries use the standard C calling convention, and are assumed to return int.

(emphasis mine.)

https://docs.python.org/2.7/library/ctypes.html#return-types is your friend (and the top of the page will tell you that Python2 is dead and you shouldn't use it, even if you insist on it. I'm sure you have a better reason than the Python developers themselves!).

my_c_fun = CDLL("sum_and_multiply_c.so")
sam_c = my_c_fun.sam_c
sam_c.restype = c_double
sam_c.argtypes = [ c_int ]
value = sam_c(6000)
print(value)

is the way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文