#include "iostream"
using namespace std;
class Calc {
public:
int add(int a, int b);
void sf();
};
int Calc::add(int a, int b) {
cout << "收到参数为a,b: " << a << " " << b << endl;
return a + b;
}
void Calc::sf() {
for (int i = 0; i < 1001; i++) {
for (int j = 0; j < 1001; j++) {
for (int k = 0; k < 1001; k++) {
if (i * i + j * j == k * k && i + j + k == 1000) {
cout << i << "," << j << "," << k << endl;
}
}
}
}
}
extern "C" {
Calc calc;
int add(int a, int b) {
return calc.add(a, b);
}
void sf() {
calc.sf();
}
};
g++ -finput-charset=UTF-8 -fexec-charset=GBK -o add.so -shared -fPIC add.cpp
import ctypes
import time
dll = ctypes.cdll.LoadLibrary
calc_lib = dll('./add.so')
r = calc_lib.add(3, 5)
print(r)
t1 = time.time()
# 运算300多秒 5分钟
# for i in range(1001):
# for j in range(1001):
# for k in range(1001):
# if i * i + j * j == k * k and i + j + k == 1000:
# print(i, j, k)
# 运行1.6s即可完成
calc_lib.sf()
t2 = time.time()
print(t2-t1)
|