from dssim_core import Engine
import time
engine = Engine()
# 创建顶层模型
model = engine.create_model()
engine.set_primary_model(model)
engine.set_parameter("step_size", 1e-3)
engine.set_parameter("start_time", 0)
engine.set_parameter("stop_time", 10)
engine.set_parameter("solver", "ode3")
# 创建系统
step = model.create_system("sources.step")
ss = model.create_system("continuous.state_space")
ss2 = model.create_system("continuous.state_space")
ss3 = model.create_system("continuous.state_space")
ss4 = model.create_system("continuous.state_space")
ss5 = model.create_system("continuous.state_space")
scope = model.create_system("sinks.scope")
# 连接系统
model.link_system(step, "output", ss, "u")
model.link_system(ss, "y", ss2, "u")
model.link_system(ss2, "y", ss3, "u")
model.link_system(ss3, "y", ss4, "u")
model.link_system(ss4, "y", ss5, "u")
model.link_system(ss5, "y", scope, "IP0")
# model.link_system(ss, "y", scope, "IP0")
# 仿真
start = time.time()
engine.simulate()
end = time.time()
print("python version run time is {}s".format(end-start))
#include <iostream>
#include <time.h>
#include "engine.hh"
int main(int args, char** argv) {
time_t start, end;
Engine engine;
Model* model = engine.createModel();
engine.setPrimaryModel(model);
BaseSystem* step = model->createSystem("step");
step->setId("step");
BaseSystem* ss = model->createSystem("state space");
BaseSystem* ss2 = model->createSystem("state space");
BaseSystem* ss3 = model->createSystem("state space");
BaseSystem* ss4 = model->createSystem("state space");
BaseSystem* ss5 = model->createSystem("state space");
ss->setId("ss");
ss2->setId("ss2");
ss3->setId("ss3");
ss4->setId("ss4");
ss5->setId("ss5");
BaseSystem* scope = model->createSystem("scope");
scope->setId("scope");
// model->linkSystem(step, "output", scope, "input");
model->linkSystem(step, "output", ss, "input");
// model->linkSystem(ss, "output", scope, "input");
model->linkSystem(ss, "output", ss2, "input");
model->linkSystem(ss2, "output", ss3, "input");
model->linkSystem(ss3, "output", ss4, "input");
model->linkSystem(ss4, "output", ss5, "input");
model->linkSystem(ss5, "output", scope, "input");
start = clock();
engine.simulate();
end = clock();
std::cout << "C++ version run time is " << (end - start) / 1000000.0 << "s" << std::endl;
return 0;
}
根据四组测试结果,发现cpp版比python快10倍之上。
本文章使用limfx的vscode插件快速发布