簡體   English   中英

為什么python插入數據庫時​​比c++快?

[英]why is python faster than c ++ when inserting into a database?

我有一個由 100 個元素組成的數組,稱為“項目”

Python(大約需要 1 秒):

for item in items:
           cur.execute("INSERT INTO list VALUES (?)",(item,))
db.commit()

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++ 沒有prepare (9 秒):

for ( auto it = items.begin(); it != items.end(); ++it  ) {
    std::string item = *it;
    qry.exec("INSERT INTO list VALUES ('"+item+"')");

基本上我的問題是是否有一種方法可以像在 Python 中一樣快地在 C++ 中使用insert

這是C++中快速批量插入的正確方法

花不到 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM