// mydll.c
__declspec(dllexport) double test(double a, double b)
{
return a / b - 1;
}
// test.java
import com.sun.jna.Native;
import com.sun.jna.Library;
public class test {
public interface DLL extends Library {
DLL INSTANCE = (DLL)Native.load("mydll", DLL.class);
double test(double a, double b);
}
public static void main(String[] args) {
DLL dll = DLL.INSTANCE;
long st = System.nanoTime();
dll._conformance(3.234, 4.234);
System.out.println("耗时: " + String.valueOf(System.nanoTime() - st));
}
}
// 耗时: 6795340
import time
from ctypes import *
dll = CDLL("mydll.dll")
dll.test.restype = c_double
def invoke():
st = time.time_ns()
dll.test(c_double(3.234), c_double(4.234))
print("耗时: ", time.time_ns() - st)
invoke()
# 耗时: 0
为什么 jna 调用一次 dll函数,都是 毫秒级的?怎么样能达到 python 那样的调用速度?