简体   繁体   English

可以加速PHP-CLI脚本吗?

[英]Possibilities to speed up PHP-CLI script?

I wrote a PHP-CLI script that mixes two audio (.WAV PCM) files (with some math involved) so PHP needs to crunch through thousands (if not even millions) of samples with unpack() , do math on them and save them with pack() . 我编写了一个PHP-CLI脚本,该脚本混合了两个音频(.WAV PCM)文件(涉及一些数学运算),因此PHP需要使用unpack()成千上万个(即使不是数百万个)样本,对它们进行数学运算并保存它们与pack()

Now, I dont need actual info on how to do the mixing or anything, as the title says, I'm looking for possibilites to speed this process up since the script needs 30 seconds of processing time to produce 10 seconds of audio output. 现在,我不需要有关如何进行混合或其他操作的实际信息,正如标题所述,我正在寻找可能的方法来加快此过程,因为脚本需要30秒的处理时间才能产生10秒的音频输出。

Things that I tried: 我尝试过的事情:

  • Cache the audiofiles to memory and crunch through with substr() instead of fseek() / fread() . 将音频文件缓存到内存中,然后使用substr()而不是fseek() / fread() Performance gain: 3 seconds. 性能提升:3秒。
  • Write the output file in 5000-samples chunks. 以5000个样本的块写入输出文件。 Performance gain: 10 seconds. 性能提升:10秒。

After those optimizations I ended up at approximately 17 seconds processing time for 10 seconds audio output. 经过这些优化之后,我以大约17秒的处理时间结束了10秒钟的音频输出。 What bugs me, is that other tools can do simple audio operations like mixing two files in realtime or even much faster. 让我感到困扰的是,其他工具可以执行简单的音频操作,例如实时混合两个文件甚至更快。

Another idea I had was paralellization, but I refrained from that due to the extra problems that would occur (like calculating correct seek positions for the forks/threads and other related things). 我的另一个想法是并行化,但是由于会发生额外的问题(例如为fork / threads和其他相关事物计算正确的查找位置),所以我避免了这种做法。

So am I missing stuff out or is this actually good performance for a PHP-CLI script? 那么,我是否错过了什么?对于PHP-CLI脚本,这实际上是不错的性能吗?

Thanks for everyone's input on this one. 感谢大家对此的投入。

I rewrote the thing in C++ and can now perform the above actions in less than a second. 我用C ++重写了这个东西,现在可以在不到一秒钟的时间内执行上述操作。

I'd never have thought that the speed difference is that huge (compiled application is ~40X faster). 我从未想过速度差异如此之大(编译的应用程序快40倍)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM