如何有效地管理C ++中的内存/时间? [英] How to efficiently manage memory/time in C++?

查看:99
本文介绍了如何有效地管理C ++中的内存/时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这种情况:

我在Java中写了一个minimax算法,然后我在C ++中移植了代码,保留了相同的类和方法。然后我通过在析构函数中放置 delete 函数来修复C ++代码,直到所有的内存泄漏都被修复。当所有的泄漏都是固定的,我测试了一个井字游戏的算法,然后代码在C ++中移植相同。我确信C ++版本会更有效率,但是我惊讶地发现100个游戏的实例(非随机)的实例在125秒内被C ++版本解决,而Java版本在30秒内解决了它们!
然后,使用系统监视器我检查了两个程序的内存使用情况:测试结束时的Java代码内存使用增加了大约20%,而使用C ++版本的内存只增加了5%。

I wrote a minimax algorithm in Java, then I ported the code in C++, mantaining the same classes and methods. Then I valgrinded the C++ code until all memory leaks were fixed by putting delete functions in destructors (when possible). When all leaks were fixed, I tested the algorithm on a tic-tac-toe game, which code was then ported identically in C++. I was sure that the C++ version would have been more performant, however I was surprised seeing that 100 (not random) instances of the game were solved in 125 seconds by C++ version while the Java version solved them in 30 seconds! Then, using System Monitor I checked the memory usage of the two programs: the Java code memory usage at the end of the test increased roughly 20% while with the C++ version the memory only increased of 5%.

然后,在我看来,这个 delete 策略是内存节省,但杀死时间性能,这不是我想,至少在这种情况下。有没有另一个删除策略来更好地设计需要小运行时但允许高内存使用的软件?

Then, at my eyes, it's clear that this delete policy is memory saving but kills time performance, which is not what I want, at least in this case. Is there another deletion policy to better design softwares which requires small runtime but allow high memory usage?

推荐答案

malloc和delete更多工作为

malloc and delete have to do more work as


  • 内存不是以多线程方式分配


  • 删除在当前线程中执行。

  • 对于64位应用程序,您可能会发现内存对齐是16字节,而不是8字节,导致每个分配更多的填充。

  • memory is not allocates in a multi-threaded way
  • memory is not allocated continuously in memory, but for regions of free memory.
  • delete is performed in the current thread.
  • for 64-bit applications you may find the memory alignment is 16 bytes instead of 8 bytes, resulting in more padding per allocation.

C ++中的可能解决方案

Possible solutions in C++


  • 不要使用堆这么多,在堆栈上分配比Java或C ++的堆,并且它是多线程的。

  • 如果可能,分配一块对象对象数组是C ++中的一个分配,而不是Java中的N + 1。

  • 使用多线程分配器。 C ++支持多个分配器。

  • 使用竞技场分配模式。如果您有一个包含大量节点的大型数据结构,则可以一次分配N个节点的块,当您释放这些节点时,可以构建一个自由节点的链接列表。你需要这样做与节点本身。在这种方法中,整个数据结构/领域都被立即解除分配。

  • don't use the heap so much, allocating on the stack is much faster than Java's or C++'s heap, and it is multi-threaded.
  • allocate blocks of objects at once if possible e.g. an array of objects is one allocation in C++ instead of N+1 in Java.
  • use a multi-threaded allocator. C++ supports multiple allocators.
  • use an arena allocation pattern. If you have a large data structure with lots of nodes, you can allocate blocks of N node at a time and when you free such nodes, build a linked list of free node. You need to do this with the nodes themselves. In this approach the whole data structure/arena is deallocated at once.

简而言之,如果你对Java代码C ++,它可能会更慢,因为这不利用所有的优化C ++允许,但Java不。

In short, if you do a literal translation of Java code to C++, it may well be slower as this doesn't take advantage of all the optimisations C++ allows but Java doesn't.

BTW Java 6+使用32位引用最多32 GB的内存。如果你构建一个64位C ++应用程序,看看是否使用32位指针/引用/索引是一个选项。

BTW Java 6+ uses 32-bit references for up to 32 GB of memory. If you build a 64-bit C++ application, see if using 32-bit pointers/references/indexes is an option for you.

这篇关于如何有效地管理C ++中的内存/时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆