帮助提高地图遍历效率

发布于 2024-11-07 07:32:36 字数 895 浏览 0 评论 0原文

我定义了一个地图,因为

map<string,map<string,int> > subjectCodes;

每个主题字符串都有自己的课程地图

我还定义了 2 个迭代器

map<string,map<string,int> >::iterator it;
map<string,int>::iterator jt;

,一个迭代器用于迭代每个主题,另一个迭代器用于迭代每个主题的每门课程

我需要让我的程序读取 50,000 行信息,排序将它们放入地图中,并在 1 秒内全部打印出来。我想我已经找到了将所有内容添加到地图中的最快方法,但我正在努力加快打印速度,目前打印速度为 0(n 平方),导致我的程序需要大约 3 秒才能运行。

这是我的打印代码:

//print out sorted list
for(it=subjectCodes.begin();it!=subjectCodes.end();it++)
{
    cout<<it->first<<": "<<(it->second).size()<<" courses"<<endl;
    for(jt=(it->second).begin();jt!=(it->second).end();jt++)
    {
        cout<<"  "<<jt->first<<": "<<jt->second<<" classes"<<endl;
    }
}

是否有一种更有效的方法可以在有人可以向我展示的地图中打印地图?谢谢

I have a map defined as

map<string,map<string,int> > subjectCodes;

each subject string has its own map of courses

I also have 2 iterators defined

map<string,map<string,int> >::iterator it;
map<string,int>::iterator jt;

one to iterate thru each subject and one to iterate thru each course per subject

I need to make my program read in 50,000 lines of info, sort them into the map, and print all in under 1 second. I think I have figured out the fastest way to add everything into the map, but I'm struggling to speed up the printing, which is 0(n squared) at the moment and causes my program to take around 3 seconds to run.

here is my print code:

//print out sorted list
for(it=subjectCodes.begin();it!=subjectCodes.end();it++)
{
    cout<<it->first<<": "<<(it->second).size()<<" courses"<<endl;
    for(jt=(it->second).begin();jt!=(it->second).end();jt++)
    {
        cout<<"  "<<jt->first<<": "<<jt->second<<" classes"<<endl;
    }
}

is there a more efficient way of printing a map in a map that someone could show me? Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

霊感 2024-11-14 07:32:36

一个简单的效率节省:

   cout<<"  "<<jt->first<<": "<<jt->second<<" classes"<<endl;

应该是:

   cout<<"  "<<jt->first<<": "<<jt->second<<" classes"<< '\n';

endl 操纵器刷新流,如果不需要刷新,这可能是一个非常昂贵的操作。您应该能够在一分钟内轻松地将 50K 行写入流,尽管可能无法写入连接到某种终端(即 xterm 或 Windows cmd 提示窗口)的流。

A simple efficiency saving:

   cout<<"  "<<jt->first<<": "<<jt->second<<" classes"<<endl;

should be:

   cout<<"  "<<jt->first<<": "<<jt->second<<" classes"<< '\n';

The endl manipulator flushes the stream, which can be a very expensive operation, if you don't need the flush. You should easily be able to write 50K lines to a stream in a minute, though possibly not to a stream connected to a terminal of some sort (i.e. to an xterm or a Windows cmd prompt window).

始于初秋 2024-11-14 07:32:36

我无法告诉你的数据是什么样的,但使用“复合键”可能会运气更好。也就是说,不要使用充满映射的映射,而是将两个键连接在一起并将结果用作单个映射中的键。

另外,如果您在创建地图后不对其进行修改,请考虑使用排序向量(使用 std::sortstd::binary_search)。当您迭代数据时,它在内存中都是连续的,您将获得更好的缓存性能。

I can't tell what your data looks like, but you might have better luck with "composite keys." That is, instead of using a map full of maps, concatenate the two keys together and use the result as the key in a single map.

Also, if you're not modifying the map after it's created, consider using a sorted vector instead (using std::sort and std::binary_search). When you iterate the data, it's all contiguous in memory and you'll get better cache performance.

寄离 2024-11-14 07:32:36

您是否考虑过并行化您的应用程序,例如使用线程或 OpenMP?

另一个提示:printf() 函数可能比流选项更快。

另外,你编译时是否进行了全面优化?这也可能会显着提高性能。

Did you think about parallelizing your application, e.g with Threads or OpenMP?

another tip: the printf() function might be faster than streaming option.

also, did you compile with full optimizations? this might also provide a significant boost in performance.

疯了 2024-11-14 07:32:36

当您遇到性能问题时,重要的是要追求容易实现的目标。为此,您需要弄清楚算法的瓶颈在哪里。什么情况需要太长时间?

一旦你弄清楚什么需要时间,你就可以开始提出更具体的问题。通常,追求容易实现的目标意味着您应该追求容易解决的问题,这些问题对算法的速度有很大影响。该线程中已经指出了两个示例(将 std::endl 替换为 '\n' 以减少刷新量,并使用 printf over std::cout 来减少函数调用/不同算法的数量)。

更多可能性:

  • 使​​用字符串流并在单个操作中编写它
  • 重新设计您的结构,以便以您通常使用它的方式使用速度更快(可以在第二级使用向量而不是映射吗?)
  • 完全不相关的东西到您编写的代码块;)

When you are having performance problems, it is important to go after the low hanging fruit. To do this, you need to figure out how where the bottlenecks of your algorithm are. What is taking too long?

Once you have figured out what is taking time, you can start asking more specific questions. Typically going after the low-hanging fruit means that you should go after easy problems to solve that have a large impact on the speed of your algorithm. Two examples of that have already been pointed out in this thread (replace std::endl by '\n' to reduce the amount of flushing and using printf over std::cout to reduce the amount of function calls/different algorithm).

A few more possibilities:

  • Use a stringstream and write that in a single operation
  • Redesign your structure so it is faster to use in the way you typically use it (could a vector be used instead of a map at the second level?)
  • Something completely unrelated to the block of code you wrote ;)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文