gpt4 book ai didi

c - Visual C 多线程文件读取

转载 作者:行者123 更新时间:2023-11-30 17:12:50 25 4
gpt4 key购买 nike

我正在解析大约 8 GB,自然我想尽可能优化我的内存和处理器使用。我正在将一个文件读入缓冲区,一次一页(4096 字节),它在大约 30 分钟内读取整个内容(比 python 快几个小时),但我想让它变得更好。我认为多线程是实现这一目标的方法,但我不知道如何在 C 中做到这一点。我看到的示例令人印象深刻,但过于复杂。我希望有人能够总结出使用 C 多线程功能的基本组件是什么,并让我了解我将遇到的任何并发问题。感谢您提供的任何帮助。

澄清一下,我正在寻找的是非常基本的东西,例如创建 2 个线程,每个线程打印 hello world 4 次。

假设我有 4 个线程搜索 81920 字节(20 页)。

thread 1: searches page 1thread 2: searches page 2thread 3: searches page 3thread 4: searches page 4---assume they finish in order---thread 1: searches page 5thread 2: searches page 6thread 3: searches page 7thread 4: searches page 8---assume they finish in order---thread 1: searches page 9thread 2: searches page 10thread 3: searches page 11thread 4: searches page 12---assume they finish in order---thread 1: searches page 13thread 2: searches page 14thread 3: searches page 15thread 4: searches page 16---assume they finish in order---thread 1: searches page 17thread 2: searches page 18thread 3: searches page 19thread 4: searches page 20

这如此重要的原因是因为对于 81920 字节中的每个字节(实际上我们希望添加大约 5 个 0,我调用 wcscpy 最多 5x

我想我找到了我要找的东西:here第二个例子是关于我正在寻找的复杂程度。我想这样就可以了,谢谢。

最佳答案

将文件映射到内存中。请参阅 MSDN 示例 "Creating a View Within a File"有关如何执行此操作的详细信息。您的用例有点不同,但原理是相同的,并且您最终将使用相同的 Windows 功能。 (如果您使用的是 64 位进程,您可能可以将整个文件映射到内存中;否则您需要将其映射为 block 并处理匹配跨越多个 block 的情况。)

您可以使用CreateThread创建多个线程来处理 block 数据。

I am reading an 8 GB binary file and searching for all occurrences of 5 specific wchar_t*

如果这就是您正在做的全部事情,那么这个工作负载几乎肯定会受到 I/O 限制,并且多个线程可能会也可能不会帮助您。这种类型的子字符串搜索可以通过对相关数据进行单次传递来完成,例如使用 Knuth-Morris-Pratt对于单个模式或 Aho-Corasick对于多种模式。

关于c - Visual C 多线程文件读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31237556/

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