Efficiency of multiplication by 2

天大地大妈咪最大 提交于 2019-12-10 10:59:17

问题


I'm supposed to allocate memory geometrically and set the initial size to 1000. When this is filled, it would expand to 2000, 4000, and so on.

My question is: If I set the initial size to multiply of 2, which is 1024, would it be any different in terms of efficiency or any other aspects?

Please don't talk about vectors and alternative methods to allocate, this is just theoretical.


回答1:


There was an article by Andrew Koenig in a 1998 issue of Journal of Object Orient Programming about buffer growth strategies. Unfortunately, I am not able to locate an online copy of the article.

In general exponential growth is preferred to fixed growth. In exponential growth a factor of 1.6 (or 1.5) is preferred. Koenig talks about the reason here in a usenet post

http://groups.google.com/group/comp.lang.c++.moderated/msg/ba558b4924758e2e?

There is a technical reason to prefer 1.5 to 2 -- more specifically, to prefer values less than (1+sqrt(5))/2.

Suppose you are using a first-fit memory allocator, and you're progressively appending to a vector. Then each time you reallocate, you allocate new memory, copy the elements, then free the old memory. That leaves a gap, and it would be nice to be able to use that memory eventually. If the vector grows too rapidly, it will always be too big for the available memory. It turns out that if the growth factor is >= (1+sqrt(5))/2, the new memory will always be too big for the hole that has been left sofar; if it is <(1+sqrt(5))/2, the new memory will eventually fit. So 1.5 is small enough to allow the memory to be recycled.

P J Plauger's STL Implementation (used by MSVC) of vector uses 1.5 based on the above.

The full thread where a lot of big C++ guys discuss it - http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/6ac1ff5688d6289c/ba558b4924758e2e#ba558b4924758e2e

Also there are a couple of articles which talk about Koenig's article in JOOP.

1) http://www.gotw.ca/gotw/043.htm

For more information, see Andrew Koenig's column in the September 1998 issue of JOOP (Journal of Object-Oriented Programming). Koenig also shows why, again in general, the best growth factor is not 2 but probably about 1.5.

2) http://www10.informatik.uni-erlangen.de/Publications/TechnicalReports/TechRep09-11.pdf

The growth policy enables the user to specify how a pointer vector grows in case it needs more elements. Although in most cases the optimal growth strategy suggested by Andrew Koenig [Koe98,Sut07] should provide the best performance for most scenarios, in some scenarios a dierent approach can still make a difference.




回答2:


I don't think it would matter with the systems I have used and optimized code for. However, it would be very dependent on the OS and compiler you use. The best way to know would be a simple benchmark code, as NPE suggested.




回答3:


The question is: Why do you think it would be different to use powers of 2? Depending on the OS, the used memory allocator and some other factors, there are differences and the only reasonable way to go ahead is benchmark it for your use case.

What you would like to test is this: Figure out how much overhead your system has per allocation. In some cases it might be helpful to the memory allocator when you reserv powers of 2 minus the overhead. As an example, if the overhead is 24 bytes, start with 1024-24=1000 bytes. If you need to increase it, use 2048-24=2024 bytes, etc.

You will also run longer sessions with lots of different allocations to see if the chunk sizes affect the fragmentation of the memory.

Of course, I don't know if this is (still) applicable to your system.



来源:https://stackoverflow.com/questions/15720280/efficiency-of-multiplication-by-2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!