gpt4 book ai didi

javascript web workers多线程字符串搜索比单线程慢?

转载 作者:行者123 更新时间:2023-11-30 15:06:04 26 4
gpt4 key购买 nike

我有一个模糊搜索功能。我有一个 52k 单词的列表。我正在针对每个单词运行该函数。大约需要 30 毫秒才能完成。

我尝试使用 myWorker.postMessage({targets:slice, search})< 将列表的 1/8 发送给每个工作人员(我的 cpu 上有 8 个线程),从而将它分成 8 个网络工作线程。但这要慢得多,大约 100 毫秒。

我的问题是:这里多线程有可能更快吗?还是在 30 毫秒线程内完成复制的数据太多了?是否可以不复制内存并拥有某种共享内存?

(似乎只是简单地将数据发送给工作人员比我实际在 1 个线程中搜索所有数据要慢!)

最佳答案

is it possible to not copy the memory and have some kind of shared memory?

您可以使用 Worker.postMessage() 的第二个参数来 transferWorker 线程到主线程,或从主线程到 worker 创建的对象。

// transfer data to `Worker` instance
worker.postMessage(data.buffer, [data.buffer]) // where `data` is an `ArrayBuffer`

// transfer data from `Worker` instance
self.postMessage(data.buffer, [data.buffer]) // where `data` is an `ArrayBuffer`

Passing data by transferring ownership (transferable objects)

Google Chrome 17+ and Firefox 18+ contain an additional way to pass certain types of objects (transferable objects, that is objects implementing the Transferable interface) to or from a worker with high performance. Transferable objects are transferred from one context to another with a zero-copy operation, which results in a vast performance improvement when sending large data sets.

关于javascript web workers多线程字符串搜索比单线程慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45728454/

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