简体   繁体   中英

Affect on std::cout on performance of the program

I have written a program which would fetch file details and place them in the SQLite database. In this process i have observed two scenarios:

Scenario 1:

  1. Loop through each and every file and fill the file details(path) in to the database(sqlite3).
  2. While for each file encountered,keep a counter and print the same via std::cout. Time taken to execute this program:30mins

Scenario 2:

  1. Loop through each and every file and fill the file details(path) in to the database(sqlite3).

Time taken to execute this program:7mins

I am very unclear as of why just because of std::cout ,the time taken is 5 times more than that of not printing it?

Any pointers on this scenario is highly appreciated.Thanks a lot.

Regards, Ravi

尝试使用std::ios_base::sync_with_stdio(false) ,默认情况下std :: cout与stdio同步

Printing to console is costly. Usually more time-consuming than writing to a file.

I bet your code could run in less than a minute. Make sure you surround some inserts in a transaction . You can also use multiple inserts in one statement, like explained in answers to this question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM