[英]why is python faster than c ++ when inserting into a database?
I have an array of 100 elements called "items"我有一个由 100 个元素组成的数组,称为“项目”
Python (take about 1 sec): Python(大约需要 1 秒):
for item in items:
cur.execute("INSERT INTO list VALUES (?)",(item,))
db.commit()
C++ (9 sec): C++(9 秒):
for ( auto it = items.begin(); it != items.end(); ++it ) {
qry.prepare("INSERT INTO list VALUES (?)");
std::string item = *it;
qry.addBindValue(item);
qry.exec();
C++ without prepare
(9 sec): C++ 没有
prepare
(9 秒):
for ( auto it = items.begin(); it != items.end(); ++it ) {
std::string item = *it;
qry.exec("INSERT INTO list VALUES ('"+item+"')");
Basically my question is whether there is a way to use insert
in C++ as fast as in Python.基本上我的问题是是否有一种方法可以像在 Python 中一样快地在 C++ 中使用
insert
。
This is a correct way for fast batch inserts in C++这是C++中快速批量插入的正确方法
Take less than 1 sec:花不到 1 秒的时间:
db.transaction();
for ( auto it = items.begin(); it != items.end(); ++it ) {
std::string item = *it;
qry.exec("INSERT INTO list VALUES ('"+item+"')");
}
db.commit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.