Javascript 预期网络工作者的速度

Javascript 预期网络工作者的速度,javascript,multithreading,web-worker,Javascript,Multithreading,Web Worker,我需要计算数学密集型函数多次(>10000次)。我想我应该使用网络工作者来减少计算时间 我正在使用一个函数,该函数创建函数的blob,将其执行包装在承诺中,并在blob处运行web worker。我对它进行了测试,它可以工作,但运行速度明显慢于单线程方法 单线程:3毫秒 多线程:5524毫秒 完整的代码,包括时间测试: 此代码计算前n个正方形并将它们推送到一个数组中。控制台显示运行单线程和多线程的相应时间 这是我用来向网络工作者宣传的功能。是否有什么问题导致执行时间比简单的for循环多得多 f

我需要计算数学密集型函数多次(>10000次)。我想我应该使用网络工作者来减少计算时间

我正在使用一个函数,该函数创建函数的blob,将其执行包装在承诺中,并在blob处运行web worker。我对它进行了测试,它可以工作,但运行速度明显慢于单线程方法

单线程:3毫秒 多线程:5524毫秒

完整的代码,包括时间测试:

此代码计算前n个正方形并将它们推送到一个数组中。控制台显示运行单线程和多线程的相应时间

这是我用来向网络工作者宣传的功能。是否有什么问题导致执行时间比简单的for循环多得多

function thread(fn){
  return function(args){
    return new Promise(function(resolve) {

        var worker=new Worker(URL.createObjectURL(new Blob(['('+fn+')('+JSON.stringify(args)+')'])));
        worker.postMessage(args)
        worker.onmessage = function(event){
            resolve(event.data);
            worker.terminate();
        };
    });
  }
 }
谢谢你的帮助

以下是有关我的CPU的信息,以防出现问题:


您忽略了web工作人员的严重开销。难怪通过字符串化函数创建blob、从该文件创建辅助程序、解析辅助程序的代码、实例化承诺、发送消息、设置侦听器并异步等待辅助程序的结果的代码要比执行两倍的单次乘法慢几千倍。事实上,我非常惊讶,你甚至在5秒钟内就成功地产生了10000名工人

估计设置一名工人的时间约为40毫秒。所以不,工作线程不是轻量级线程,您应该为任何事情生成它。它们被定义为工作线程,您应该向它们发送(许多)消息以进行处理和响应。您可能希望创建一个工作线程池,将负载分散到多个线程上


当然,对于
square
函数,这些都不适用。它太小了——它只做一次乘法。在主线程上执行此操作将比与其他线程通信的任何操作都要快。如果在该函数中有100000个items循环,那么在后台线程中运行可能是值得的。您获得的3ms单线程性能甚至不会明显阻塞主线程。

您的数学密集型函数是否需要10k迭代?如果是这样的话,那么10k循环对于JS来说并不密集。为什么它在web workers中运行缓慢,是因为web worker可能需要时间来生成。如果你真的想测试使用web workers的多线程与单线程之间的性能,试着运行一个迭代次数超过5百万次的for循环(这对于JS来说是非常密集的),然后看看游戏是如何改变的。你在论文中至少证明了两个事实:1)这:
i*i
createObjectURL()不等价
然后
实例化一个Blob
然后
JSON.stringify()
然后
在Worker中创建函数对象
然后
在Worker中执行函数
。2)工人不是简单函数(如平方数)的理想人选。此外,如前所述,您的时间度量考虑了将结果记录到控制台所需的时间量。作为实验,删除控制台日志。我敢打赌,单线程版本将变为零毫秒。
uinfo 
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 76
model name  : Intel(R) Atom(TM) x7-Z8700  CPU @ 1.60GHz
stepping    : 3
microcode   : 0x367
cpu MHz     : 901.401
cache size  : 1024 KB
physical id : 0
siblings    : 4
core id     : 0
cpu cores   : 4
apicid      : 0
initial apicid  : 0
fpu     : yes
fpu_exception   : yes
cpuid level : 11
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes rdrand lahf_lm 3dnowprefetch epb pti ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid tsc_adjust smep erms dtherm ida arat
bugs        : cpu_meltdown spectre_v1 spectre_v2
bogomips    : 3200.00
clflush size    : 64
cache_alignment : 64
address sizes   : 36 bits physical, 48 bits virtual
power management:

processor  : 1
vendor_id   : GenuineIntel
cpu family  : 6
model       : 76
model name  : Intel(R) Atom(TM) x7-Z8700  CPU @ 1.60GHz
stepping    : 3
microcode   : 0x367
cpu MHz     : 875.272
cache size  : 1024 KB
physical id : 0
siblings    : 4
core id     : 1
cpu cores   : 4
apicid      : 2
initial apicid  : 2
fpu     : yes
fpu_exception   : yes
cpuid level : 11
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes rdrand lahf_lm 3dnowprefetch epb pti ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid tsc_adjust smep erms dtherm ida arat
bugs        : cpu_meltdown spectre_v1 spectre_v2
bogomips    : 3200.00
clflush size    : 64
cache_alignment : 64
address sizes   : 36 bits physical, 48 bits virtual
power management:

 processor : 2
vendor_id   : GenuineIntel
cpu family  : 6
model       : 76
model name  : Intel(R) Atom(TM) x7-Z8700  CPU @ 1.60GHz
stepping    : 3
microcode   : 0x367
cpu MHz     : 860.525
cache size  : 1024 KB
physical id : 0
siblings    : 4
core id     : 2
cpu cores   : 4
apicid      : 4
initial apicid  : 4
fpu     : yes
fpu_exception   : yes
cpuid level : 11
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes rdrand lahf_lm 3dnowprefetch epb pti ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid tsc_adjust smep erms dtherm ida arat
bugs        : cpu_meltdown spectre_v1 spectre_v2
bogomips    : 3200.00
clflush size    : 64
cache_alignment : 64
address sizes   : 36 bits physical, 48 bits virtual
power management:

processor  : 3
vendor_id   : GenuineIntel
cpu family  : 6
model       : 76
model name  : Intel(R) Atom(TM) x7-Z8700  CPU @ 1.60GHz
stepping    : 3
microcode   : 0x367
cpu MHz     : 557.593
cache size  : 1024 KB
physical id : 0
siblings    : 4
core id     : 3
cpu cores   : 4
apicid      : 6
initial apicid  : 6
fpu     : yes
fpu_exception   : yes
cpuid level : 11
wp      : yes
flags       : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology tsc_reliable nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes rdrand lahf_lm 3dnowprefetch epb pti ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid tsc_adjust smep erms dtherm ida arat
bugs        : cpu_meltdown spectre_v1 spectre_v2
bogomips    : 3200.00
clflush size    : 64
cache_alignment : 64
address sizes   : 36 bits physical, 48 bits virtual
power management:```