gpt4 book ai didi

malloc - g_slice 真的比 malloc 快吗

转载 作者:行者123 更新时间:2023-12-01 04:35:34 30 4
gpt4 key购买 nike

GLib 文档推荐使用 GLib Slice Allocator 而不是 malloc:

"For newly written code it is recommended to use the new g_slice API instead of g_malloc() and friends, as long as objects are not resized during their lifetime and the object size used at allocation time is still available when freeing." -- http://developer.gnome.org/glib/unstable/glib-Memory-Slices.html



但实际上 g_slice 比 Windows/Linux malloc 快得多(快到足以保证处理大小和 GLib 的预处理器黑客如 g_slice_new 的额外麻烦)?我计划在我的 C++ 程序中使用 GLib 来处理 INIish 配置( GKeyFile )并访问 C++ 中不可用的数据结构,如 GHashTable ,所以无论如何 GLib 依赖都无关紧要。

最佳答案

足够快以值得它取决于您的应用程序。但他们应该更快。

除了速度之外还有另一个问题,那就是内存碎片和每块开销。切片
让 malloc 处理大的或可变大小的分配,同时更节省空间地处理小的已知大小的对象。

关于malloc - g_slice 真的比 malloc 快吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6571781/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com