{"id":904,"date":"2022-07-15T20:13:51","date_gmt":"2022-07-15T17:13:51","guid":{"rendered":"https:\/\/www.juustila.com\/antti\/?p=904"},"modified":"2022-07-15T20:13:51","modified_gmt":"2022-07-15T17:13:51","slug":"mind-the-map","status":"publish","type":"post","link":"https:\/\/www.juustila.com\/antti\/2022\/07\/15\/mind-the-map\/","title":{"rendered":"Mind the map"},"content":{"rendered":"\n<p>Another post about data structures, time performance and the <em>count-the-words-in-a-book-file-fast<\/em> case I&#8217;ve been writing about before.<\/p>\n\n\n\n<p>I did a C++ implementation of the books and words problem. Earlier, I have implemented several solutions for the problem using Swift and Java. This time I used C++ standard library <code>std::map<\/code> and wanted to see if <em>parallel processing in several threads<\/em> would speed up the processing.<\/p>\n\n\n\n<p><strong>Obviously it did<\/strong>. Execution time of the multithreaded version was <strong>74%<\/strong> of the single threaded version. The sample files were processed by the single threaded version in 665 ms, while the multithreaded version took only 491 ms. Nice!<\/p>\n\n\n\n<p>But then I saw, from the documentation of the <code>std::map<\/code>, that it keeps the key values in the dictionary <em>in order<\/em> while elements are added to the map\/dictionary. <\/p>\n\n\n\n<p>But this is not needed in my case! Surely this also <em>takes time<\/em> and gives me additional possibilities in optimising the time performance.<\/p>\n\n\n\n<p>I changed, in the single threaded implementation, the <code>std::map<\/code> to <code>std::unordered_map<\/code>, and behold, it was <strong>faster than the multithreaded version<\/strong> with 446 ms execution time!<\/p>\n\n\n\n<p>So mind the map. There are many, and some of those may be more suitable to your use case than the others.<\/p>\n\n\n\n<p>For details, see the project in <a href=\"https:\/\/github.com\/anttijuu\/books-cpp\">GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Another post about data structures, time performance and the count-the-words-in-a-book-file-fast case I&#8217;ve been writing about before. I did a C++ implementation of the books and words problem. Earlier, I have implemented several solutions for the problem using Swift and Java. This time I used C++ standard library std::map and wanted to see if parallel processing &hellip; <a href=\"https:\/\/www.juustila.com\/antti\/2022\/07\/15\/mind-the-map\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Mind the map&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[2],"tags":[10,94,39,92,93,17],"class_list":["post-904","post","type-post","status-publish","format-standard","hentry","category-coding","tag-c","tag-dictionary","tag-performance","tag-stdmap","tag-stdunordered_map","tag-threads"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/comments?post=904"}],"version-history":[{"count":1,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/904\/revisions"}],"predecessor-version":[{"id":905,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/904\/revisions\/905"}],"wp:attachment":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/media?parent=904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/categories?post=904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/tags?post=904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}