Solved Is QT Automatically optimizing my loop to run on multiple cores?
-
I apologize in advance if this question seems trivial.
I have a SQL database that consists of Movies,Actors,Tags and Pictures.
A Movie and Picture tables have 'PathToFile' columns which contains the path of the movie or the picture on disk.
Tags, and Actors table, have 'name' columns which contain the tag or actor name.I have a class called Tagger whos job is to go over all pictures and movies paths and see if the path contains an actor or tag name, if it does it would add the relation to the database.
I'm testing this with over 70K pictures in the database so the process takes some time, however when I open Task Manager (I'm using windows 10.) While Tagger is doing it's work, I see the workload is evenly spread across all 4 of the CPU cores.
I didn't do any multi-threading optimization and am curious as to what is going on.
Here is the code for the loop function :
void Tagger::tag(QList<QMap<QString, QVariant> > thingsToTag, QList<QMap<QString, QVariant> > preparedTaggingElemets, QList<QStringList> &actors, QList<QStringList> &tags, QList<QStringList> &websites, QString thingToTagType) { for (int i = 0 ; i < thingsToTag.size() ; i++) { QMap<QString, QVariant> currentThingToTag = thingsToTag.at(i); QString thingToTagName = this->prepareForComparison(currentThingToTag["path_to_file"].toString()); qDebug() << "Checking " << i << " Out of " << thingsToTag.size() << " " + thingToTagType + " " << thingToTagName << " for tags..."; for (int j = 0 ; j < preparedTaggingElemets.size() ; j++) { QMap<QString, QVariant> currentTaggingElement = preparedTaggingElemets.at(j); QString nameToCheck = currentTaggingElement["name"].toString(); if (thingToTagName.contains(nameToCheck)){ qDebug() << "Found " << nameToCheck << "in" << thingToTagName; if (currentTaggingElement["table_name"] == "actor") { QStringList temp; temp.append(currentThingToTag["id"].toString()); temp.append(currentTaggingElement["id"].toString()); actors.append(temp); }else if (currentTaggingElement["table_name"] == "tag") { QStringList temp; temp.append(currentThingToTag["id"].toString()); temp.append(currentTaggingElement["id"].toString()); tags.append(temp); }else if (currentTaggingElement["table_name"] == "website") { QStringList temp; temp.append(currentThingToTag["id"].toString()); temp.append(currentTaggingElement["id"].toString()); websites.append(temp); }else if (currentTaggingElement["table_name"] == "actorAlias") { QStringList temp; temp.append(currentThingToTag["id"].toString()); temp.append(currentTaggingElement["alias_of"].toString()); actors.append(temp); }else if (currentTaggingElement["table_name"] == "tagAlias") { QStringList temp; temp.append(currentThingToTag["id"].toString()); temp.append(currentTaggingElement["alias_of"].toString()); tags.append(temp); }else if (currentTaggingElement["table_name"] == "websiteAlias") { QStringList temp; temp.append(currentThingToTag["id"].toString()); temp.append(currentTaggingElement["alias_of"].toString()); websites.append(temp); } } thingToTagName = thingToTagName.replace(nameToCheck,""); } } }
-
Mostly likely you are seeing this phenomenon
You should see for your application on the details tab. Most likely it will consume up to 25%. When you have done something to use multi-threading it may consume on optimum almost a 100%. -
@koahnig Thank you! Exactly what I wanted to know.
-
@Curtwagner1984 Just out of curiousity, are your planning to optimize your code for multicore parallelism?
-
@Eeli-K If I'll have time yes. This particular function runs fast enough in a single thread, if I'll won't have other pressing concerns I'll optimize it.
-
@Curtwagner1984 I thought this might be an opportunity to use the former experimental, nowadays standardized (C++17) parallel STL algorithms. I don't know how to use them but it surely looks interesting. You don't have to create threads, just use the algorithms or even loops like for_each and they take care of parallelizing themselves. Your data and your own algorithms must be of course designed to be thread-safe. Maybe some C++ guru can give an opinion.
-
@Eeli-K Sounds interesting